aspose file tools*
The moose likes Java in General and the fly likes How to count the number of digits after decimal point in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to count the number of digits after decimal point in java?" Watch "How to count the number of digits after decimal point in java?" New topic
Author

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

auvrm papu
Ranch Hand

Joined: Sep 01, 2005
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

Joined: Oct 09, 2006
Posts: 94
Laxi Hi!

Have you checked out the API?

String API

Or.

Regex

This could help you in your quest.

Good luck.

Nick
[ March 21, 2007: Message edited by: Nick White ]
auvrm papu
Ranch Hand

Joined: Sep 01, 2005
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

Joined: Oct 17, 2006
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.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
sindy lee
Greenhorn

Joined: Aug 05, 2010
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

Joined: Oct 13, 2005
Posts: 38357
    
  23
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

Joined: Oct 13, 2005
Posts: 38357
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to count the number of digits after decimal point in java?