Strange Result
posted 19 years ago
Instead of showing 0.90 it's showing 0.8999999999999999. Could anyone give the reason.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Instead of showing 0.90 it's showing 0.8999999999999999. Could anyone give the reason.

The Best way to predict your future is to create it - Every great individual common man
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It's because of the binary representation of a double, which holds up to 15 digits of precision.
You can get the result you want in at least two ways:
1. Convert the literals from a double to a float (4 bytes instead of 8, and roughly 7 digits of precision), as follows:
2. Format the result to print the number of digits you want, which will round appropriately, as follows:
import java.text.DecimalFormat;
For further information and examples, Google "Number Format Java"
[ April 12, 2006: Message edited by: Tom Fulton ]
You can get the result you want in at least two ways:
1. Convert the literals from a double to a float (4 bytes instead of 8, and roughly 7 digits of precision), as follows:
2. Format the result to print the number of digits you want, which will round appropriately, as follows:
import java.text.DecimalFormat;
For further information and examples, Google "Number Format Java"
[ April 12, 2006: Message edited by: Tom Fulton ]
| Crusading Chameleon likes the size of this ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








