# How to count the number of digits after decimal point in java?

auvrm papu
Ranch Hand
Posts: 105
How to count the number of digits after decimal point in java?

I have a double value stored in string..

myStr = "145.3424"
I need count = 4

also,

if
myStr = "145.3420"
it should give count = 3

and
set myStr = "145.342"

Nick White
Ranch Hand
Posts: 94
Laxi Hi!

Have you checked out the API?

String API

Or.

Regex

Good luck.

Nick
[ March 21, 2007: Message edited by: Nick White ]

auvrm papu
Ranch Hand
Posts: 105
Thanks.

is there a simple function in java doing that already?

for ex:
reverse() function for strings already exits..

similary, i was expecting some java api to fetch the decimal part from that given double value..from which I can do my length() operator.

makes sense?
[ March 21, 2007: Message edited by: Laxi Rara ]

David McCombs
Ranch Hand
Posts: 212
This can be done as easily as 2 String method calls. length() of course and for the other look up split().

Be careful of the case where no decimal point exists.

sindy lee
Greenhorn
Posts: 14
I think,you can not do it with "String", for ex.: what about the number "8.0E-5" ?

So i want to ask the same question!

Campbell Ritchie
Sheriff
Posts: 48938
60
A double does not have a decimal point. It has a mantissa, which always has a single 1 before the radix point, and an exponent, and a sign. You can Google IEEE754 for more details. So "number of places beyond the decimal point" is meaningless. The only floating-point number which actually has a decimal point is BigDecimal. But don't try converting a double to a BigDecimal, otherwise you will get a display with dozens of digits after the decimal point. I believe there are methods in BigDecimal which give you the number of places directly.

Campbell Ritchie
Sheriff
Posts: 48938
60
I earlier wrote:. . . mantissa . . . always has a single 1 before the radix point, . . .
. . . except for values with an absolute value < this, or ±0, or ∞ or "NaN". The 1 in "normal" values is implicit; it isn't actually stored in memory.