Why Java will not accept 0.5 value?(Urgent)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalyani09
    New Member
    • May 2007
    • 3

    Why Java will not accept 0.5 value?(Urgent)

    Hi,

    I have a problem in Java...Problem was...

    We have created one website called "STATS" in that website the user need to enter the Qty,Price,Date to confirm their Purchase Orders.After confirming their orders the price gets round up automatically.. .

    For Ex :Price: 4.10 for 1 Item Qty : 40
    3.06 for 2nd item Qty : 30
    14.82 for 3rd Item Qty : 20
    2.67 for 4th Item Qty : 20
    383.44 for 5th Item Qty : 1
    When the user enters the above information and select "Confirm" button ,,,then Subtotal, GST, TotalAmount will be calculated automatically.
    All the details will store in 2 SQL Tables...,,,All the price information will store in Details table..

    But now the prob was: if i give the above price and qty Subtotal will be 988.89,,GST will be 49.44,,Total Price will be 1038.33..

    But when i calculate with calculator the exact price must be as : Sub total : 989.04,,GST : 49.45,,,TotalPr ice : 1038.49...

    Why the Java compiler is rounding the value..can we solve this problem.???

    We are using Java Platform/2003 Os/JDK 1.3 version and the Database SQL 2000 Developer Edition,,and also JRUN 3.0..


    please help on this>..

    Kalyani
  • sumittyagi
    Recognized Expert New Member
    • Mar 2007
    • 202

    #2
    Originally posted by kalyani09
    Hi,

    I have a problem in Java...Problem was...

    We have created one website called "STATS" in that website the user need to enter the Qty,Price,Date to confirm their Purchase Orders.After confirming their orders the price gets round up automatically.. .

    For Ex :Price: 4.10 for 1 Item Qty : 40
    3.06 for 2nd item Qty : 30
    14.82 for 3rd Item Qty : 20
    2.67 for 4th Item Qty : 20
    383.44 for 5th Item Qty : 1
    When the user enters the above information and select "Confirm" button ,,,then Subtotal, GST, TotalAmount will be calculated automatically.
    All the details will store in 2 SQL Tables...,,,All the price information will store in Details table..

    But now the prob was: if i give the above price and qty Subtotal will be 988.89,,GST will be 49.44,,Total Price will be 1038.33..

    But when i calculate with calculator the exact price must be as : Sub total : 989.04,,GST : 49.45,,,TotalPr ice : 1038.49...

    Why the Java compiler is rounding the value..can we solve this problem.???

    We are using Java Platform/2003 Os/JDK 1.3 version and the Database SQL 2000 Developer Edition,,and also JRUN 3.0..


    please help on this>..

    Kalyani
    try using strictfp keyword.
    Java do some optimizations according to the platform for floating point calculations. if you use strictfp, those optimizations will not be done. I am not sure but this should solve your problem.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Read the article: what every scientist should know about floating point arithmetic .

      kind regards,

      Jos

      Comment

      • kalyani09
        New Member
        • May 2007
        • 3

        #4
        tks for ur reply but now after i read i felt bit confused that article....so can u help me in any another way.

        regards,
        Kalyani

        Originally posted by JosAH

        Comment

        • kalyani09
          New Member
          • May 2007
          • 3

          #5
          tks,will try this option and let u know

          Originally posted by sumittyagi
          try using strictfp keyword.
          Java do some optimizations according to the platform for floating point calculations. if you use strictfp, those optimizations will not be done. I am not sure but this should solve your problem.

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by kalyani09
            tks,will try this option and let u know
            No, strictfp will not help you get the desired output. Some values cannot be exactly represented in binary, like 1/10. Just like in our decimal number system, we cannot write 1/3 (0.33333333...) . So, Java tries to represent 1/10 as precise as possible, but when performing multiple calculation on those floating point numbers, you get roundoff error like you encountered. To over come this, you should use the java.math.BigDe cimal class.
            Try this code snippet:
            Code:
            import java.math.BigDecimal;
            
            public class Main {
                
                public static void main(String[] args) {
                    
                    double a = 0.0;
                    BigDecimal b = new BigDecimal("0.0");
                    
                    for(int i = 1; i <= 10; i++) {
                        a = a + 0.1;
                        b = b.add(new BigDecimal("0.1"));
                    }
                    
                    System.out.println("double a     == "+a);
                    System.out.println("BigDecimal b == "+b);
                }
            }
            Good luck.

            Comment

            Working...