Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of Decimal Places

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
Is there a function in the Framework that will return the number of decimal places in a decimal or double?

NumberOfDecimals(1.1234) returns 4, NumberOfDecimalPlaces(1.120) returns 2, etc.

I could convert to a string and use the index of ".", but I would like to use the "proper" method, if one exists.


Thanks



David
[pipe]
 
I didn't see anything like that.

I think you'll have to call .ToString() on it, then find the position of the decimal point relative to the length of the string (accounting for cultural differences in how the decimal point is represented and possible negative signs)

The decimal point representation can be found at CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator. The negative sign representation can be found at CurrentThread.CurrentCulture.NumberFormat.NegativeSign.

You can also see how negative numbers are represented at CurrentThread.CurrentCulture.NumberFormat.NumberNegativePattern (since the negative sign isn't always at the front of the number). This isn't a huge problem, as you would just need a count of how many character positions it occupies in front the decimal point.
[tt]
NumberNegativePattern Display Count
====================== ======= ======
0 (n) 1
1 -n 1
2 - n 2
3 n- 0
4 n - 0
[/tt]

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top