why does this happen?

J

Jeremy Watts

i have declared a Java BigDecimal as being '0.0007' (not really that big a
decimal, but this is just an example) , and then converted this to a string.

On printing out the string I receive '0.00069999999999999288......'
(followed by a load more numbers). Why isnt it simply returning '0.0007' ?


Jeremy
 
I

Ingo R. Homann

Hi Jeremy,

Jeremy said:
i have declared a Java BigDecimal as being '0.0007' (not really that big a
decimal, but this is just an example) , and then converted this to a string.

How did you declare it?

new BigDecimal(0.0007)

will not work, because 0.0007 in the code above is a double which has a
limited precision. Have you tried

new BigDecimal("0.0007")

?

Ciao,
Ingo
 
T

Thomas Weidenfeller

Jeremy said:
On printing out the string I receive '0.00069999999999999288......'
(followed by a load more numbers). Why isnt it simply returning '0.0007' ?

This is described in the API documentation in great length. Please read
it. The precision of floating point numbers have also been discussed
here ad nauseam. You might want to search an archive of this group.

/Thomas
 
J

Jeremy Watts

Ingo R. Homann said:
Hi Jeremy,

string.

How did you declare it?

new BigDecimal(0.0007)

will not work, because 0.0007 in the code above is a double which has a
limited precision. Have you tried

new BigDecimal("0.0007")

yeah ingo, that worked great thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top