Results 1 to 5 of 5
- 08-14-2011, 06:32 AM #1
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
-
This is not a Java error, but a general problem of programming languages trying to represent floating point numbers using digital hardware -- no language can represent the numbers with perfect precision as it just can't be done. So you must take care when doing floating point calculations to take this into consideration. One solution is to format the out put so that it makes sense. So:
Java Code:import java.text.DecimalFormat; public class Foo002 { public static void main(String[] args) { double d = 0.29 * 100; System.out.println("as a double: " + d); System.out.println("don't cast the double as an int: " + (int)d); DecimalFormat decFormat = new DecimalFormat("0"); System.out.println("instead, format the number: " + decFormat.format(d)); decFormat.applyPattern("0.00000"); System.out.println("and a more accurate format: " + decFormat.format(d)); } }Last edited by Fubarable; 08-14-2011 at 06:44 AM.
- 08-15-2011, 05:29 AM #3
Try this, this seems to correct the problem.
String.format( "%.0f", 0.29 * 100 );
- 08-15-2011, 02:31 PM #4
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-15-2011, 02:42 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
Binary floating point representations, like float and double cannot represent values like 0.1 and 0.01 exactly; the value stored will always be a little more or a little less than what you might think or intend. So truncating rather than rounding can cause undesirable results.
Output:Java Code:double doubleValue = 0.29 * 100; System.out.println("doubleValue = " + String.format("%.15f", doubleValue)); int truncatedValue = (int) doubleValue; System.out.println("truncatedValue = " + truncatedValue); long roundedValue = Math.round(doubleValue); System.out.println("roundedValue = " + roundedValue);Java Code:doubleValue = 28.999999999999996 truncatedValue = 28 roundedValue = 29


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks