Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By KevinWorkman

Thread: Why (int)(0.29 * 100) = 28 in Java?

  1. #1
    rootnet47 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Lightbulb Why (int)(0.29 * 100) = 28 in Java?

    (int)(0.29 * 100) = 28
    Could anybody explain me that why Java results unexpected in such case? But in case of
    (int)(0.29f * 100) = 29. whether 0.29 itself is a double in default.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.
    rootnet47 likes this.

  3. #3
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Try this, this seems to correct the problem.

    String.format( "%.0f", 0.29 * 100 );

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    Quote Originally Posted by stchman View Post
    Try this, this seems to correct the problem.

    String.format( "%.0f", 0.29 * 100 );
    That only "corrects the problem" in the same way that casting it to a float does. Fubarable already asked the question, but for more information, google "what every computer scientist should know about floating point arithmetic".
    JeffGrigg likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    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.

    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);
    Output:
    Java Code:
    doubleValue = 28.999999999999996
    truncatedValue = 28
    roundedValue = 29

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •