# Count number of digits after decimal point

### Question

• Hi.

I am presenting a decimal in a textbox, and i am using the Strings.FormatNumber Method to make it look nice
But the problem is that the number of digits after the decimal point is varying. I need to display all of them. If i set the NumDigitsAfterDecimal parameter to -1 it uses my computer's regional settings, which is 2.
If the NumDigitsAfterDecimal is set to more than the number of digits after my decimal point, it adds trailing zeros.
Is there a way to count the number of digits after the decimal point, so i can set the NumDigitsAfterDecimal parameter "dynamically" ?
Tuesday, July 24, 2007 9:09 AM

•

Hi,

You could do one of two things whether the decimal marker is a period ( full-stop ) or a comma. Just add one button for either code example.

You could use Math.Round to round to so many decimal places like this.>>

Code Snippet

Public Class Form1

'This should be one line of code in your code window.>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim exampleNumber As Double = 23.987654321

'Output rounded to 4 decimal places.

MessageBox.Show(DecimalPlaces(exampleNumber, 4))

'or just use MATH.ROUND , this is 3 decimal places.>>>>

MessageBox.Show(Math.Round(exampleNumber, 3).ToString)

End Sub

'This should be one line of code in your code window.>>

Public Function DecimalPlaces(ByVal aDouble As Double, ByVal noOfDigits As Integer) As String

End Function

End Class

'or just show so many characters from the left of the string using SubString like this.>>

Code Snippet

Public Class Form1

'This should be one line of code in your code window.>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim exampleNumber As Double = 23.987654321

'Output rounded to 4 decimal places.

MessageBox.Show(DecimalPlaces(exampleNumber, 4))

'or just use MATH.ROUND , this is 3 decimal places.>>>>

MessageBox.Show(Math.Round(exampleNumber, 3).ToString)

'Show 7 characters from the left including the "." or ","

MessageBox.Show(exampleNumber.ToString.Substring(0, 7))

End Sub

'This should be one line of code in your code window.>>

Public Function DecimalPlaces(ByVal aDouble As Double, ByVal noOfDigits As Integer) As String

End Function

End Class

Regards,

S_DS

Wednesday, July 25, 2007 7:06 AM

• Hi, try this code [optimizations needed] :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim digit As Decimal
Try
' For example, TextBox1.Text = "123.123456"
digit = Convert.ToDecimal(TextBox1.Text)                ' digit = 123.123456

Dim idigit As Int32 = Math.Floor(digit)                 ' idigit = 123
Dim newdigit As Decimal

newdigit = (digit - idigit)                             ' newdigit = 0.123456

Dim digitstring As String
digitstring = Convert.ToString(newdigit).TrimEnd("0"c)  ' digitstring = 0.123456
MessageBox.Show(digitstring.Length - 2)                 ' digitstring.Length - 2 = 6
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Friday, July 27, 2007 11:29 AM

### All replies

• I am getting a shuddering feeling when I suggest this since I get the feeling it is not exactly clean...

but the only way I can think of counting the digits is converting the number to a string, finding the decimal character, get the substring (after the decimal), and then the length. You could shorten that a bit by subtracting the integer portion from the full number and then convert to string and get length.

Maybe somebody out there can suggest some other more efficient solution.

- Jason

Tuesday, July 24, 2007 1:47 PM
• I don't see what's so wrong with this method since the OP is already converting it to a string to display in a TextBox to begin with, It's one thing if you wan't to do some math with it, another if you just want to display it.  Any way to go a little further on Jason's post here is a function which will return the number of decimal places as an Integer

Code Snippet

Private Function DecPlaces(ByVal Number As Decimal) As Integer

Dim mNumber As String = CStr(Number)

Return mNumber.Length - InStr(mNumber, ".")

End Function

You could just use the CStr() twice and forego the function but why type the same code over and over again when you can shorten it considerably an not have as many chances for a typo.  Anyway hope this helps.

Tuesday, July 24, 2007 6:14 PM
• Try using the String.Format() method instead.  Depending on just how nice you want the number to be displayed, you might get away with something as simple as:

FormattedNumber = String.Format("{0:n}", DecimalNumber)

String.Format() Method

Standard Numeric Format Strings

Custom Numeric Format Strings

Once of the overloads of String.Format also allows you to specify the format provider so that you can override regional formatting if necessary.

Tuesday, July 24, 2007 6:30 PM
• I considered solving the problem exactly as you both suggested. But as you said, it is not an ideal solution..
Then i run into another problem.. The decimal point is varying, depending on the regional settings. For example in here in Norway we use comma (,) instead of period (.).. So I would then have to figure out what the users computers decimal point is.

I am doing som math with it, but that is no problem, because I still have it stored in the variable.. The textbox is ReadOnly. Just for displaying results.

Tuesday, July 24, 2007 6:30 PM
•

Hi,

You could do one of two things whether the decimal marker is a period ( full-stop ) or a comma. Just add one button for either code example.

You could use Math.Round to round to so many decimal places like this.>>

Code Snippet

Public Class Form1

'This should be one line of code in your code window.>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim exampleNumber As Double = 23.987654321

'Output rounded to 4 decimal places.

MessageBox.Show(DecimalPlaces(exampleNumber, 4))

'or just use MATH.ROUND , this is 3 decimal places.>>>>

MessageBox.Show(Math.Round(exampleNumber, 3).ToString)

End Sub

'This should be one line of code in your code window.>>

Public Function DecimalPlaces(ByVal aDouble As Double, ByVal noOfDigits As Integer) As String

End Function

End Class

'or just show so many characters from the left of the string using SubString like this.>>

Code Snippet

Public Class Form1

'This should be one line of code in your code window.>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim exampleNumber As Double = 23.987654321

'Output rounded to 4 decimal places.

MessageBox.Show(DecimalPlaces(exampleNumber, 4))

'or just use MATH.ROUND , this is 3 decimal places.>>>>

MessageBox.Show(Math.Round(exampleNumber, 3).ToString)

'Show 7 characters from the left including the "." or ","

MessageBox.Show(exampleNumber.ToString.Substring(0, 7))

End Sub

'This should be one line of code in your code window.>>

Public Function DecimalPlaces(ByVal aDouble As Double, ByVal noOfDigits As Integer) As String

End Function

End Class

Regards,

S_DS

Wednesday, July 25, 2007 7:06 AM

• Hi, try this code [optimizations needed] :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim digit As Decimal
Try
' For example, TextBox1.Text = "123.123456"
digit = Convert.ToDecimal(TextBox1.Text)                ' digit = 123.123456

Dim idigit As Int32 = Math.Floor(digit)                 ' idigit = 123
Dim newdigit As Decimal

newdigit = (digit - idigit)                             ' newdigit = 0.123456

Dim digitstring As String
digitstring = Convert.ToString(newdigit).TrimEnd("0"c)  ' digitstring = 0.123456
MessageBox.Show(digitstring.Length - 2)                 ' digitstring.Length - 2 = 6
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Friday, July 27, 2007 11:29 AM