Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to count the number of digits in an integer with VB.NET

2 Answers

0 votes
Imports System

Module DigitCounterLog10

    ' Counts digits using Math.Log10.
    ' Uses the formula: floor(log10(n)) + 1
    ' Zero is handled separately because log10(0) is undefined.
    Function CountDigitsLog10(value As Integer) As Integer
        Dim num As Integer = Math.Abs(value)

        If num = 0 Then
            Return 1
        End If

        Return CInt(Math.Floor(Math.Log10(num))) + 1
    End Function

    Sub Main()
        Dim number As Integer = 987654321

        Dim digits As Integer = CountDigitsLog10(number)

        Console.WriteLine("Number: " & number)
        Console.WriteLine("Digit count (log10 method): " & digits)
    End Sub

End Module


' run:
' 
' Number: 987654321
' Digit count (log10 method): 9
'

 



answered Jun 5, 2020 by avibootz
edited 4 hours ago by avibootz
0 votes
Imports System

Module DigitCounterString

    ' Returns the number of digits in an Integer using string conversion.
    ' This approach is clear, safe, and works well for everyday use.
    Function CountDigitsString(value As Integer) As Integer
        Dim text As String = value.ToString()

        ' If the number is negative, ignore the leading "-" sign
        If text.StartsWith("-") Then
            Return text.Length - 1
        End If

        Return text.Length
    End Function

    Sub Main()
        Dim number As Integer = -12345

        Dim digits As Integer = CountDigitsString(number)

        Console.WriteLine("Number: " & number)
        Console.WriteLine("Digit count (String method): " & digits)
    End Sub

End Module



' run:
'
' Number: -12345
' Digit count (String method): 5
'

 



answered 4 hours ago by avibootz
...