• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic