Ask Experts Questions for FREE Help !
Ask
    yugandhar's Avatar
    yugandhar Posts: 2, Reputation: 1
    New Member
     
    #1

    Mar 15, 2009, 03:51 AM
    Calculation in java code
    In my program total of fields is 53.25 but it is giving 53.24999

    Why it is happening like that
    yugandhar's Avatar
    yugandhar Posts: 2, Reputation: 1
    New Member
     
    #2

    Mar 15, 2009, 03:53 AM
    Quote Originally Posted by yugandhar View Post
    In my program total of fields is 53.25 but it is giving 53.24999

    why it is happening like that
    :confused:
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #3

    Mar 15, 2009, 07:54 PM

    This happens not only in Java code, but in almost all computer code. The problem is a very common one. The problem is that it's not always possible to represent a floating-point decimal number in a binary representation. You're seeing a "round off error".

    You can take steps to minimize it, like using 8-byte floating point numbers instead of 4-byte floating-point numbers. That will help, but it may not eliminate it entirely.

    When you do comparisons, the usual "fix" is to use "not equal" instead of "equal" comparisons or to compare with a value that is slightly larger than the value you expect.
    KISS's Avatar
    KISS Posts: 12,510, Reputation: 839
    Uber Member
     
    #4

    Mar 15, 2009, 08:26 PM

    If you want two places and the result is positive then take the number and multiply by 100 and add 0.5. Take the FIX() of it and divide by 100

    e.g. 53.24999;
    *100 = 5324.999
    +0.5= 5325.49
    Fix() = 5325.
    /100 = 53.25

    you have to watch integer () which is the greatest integer in x function and truncation. I forget what works for negative numbers. You may have to use the sgn() function.
    Functions may not be JAVA.

    This is where formatted output is useful.
    jstrike's Avatar
    jstrike Posts: 418, Reputation: 44
    Full Member
     
    #5

    Mar 18, 2009, 12:44 PM
    Perito gave a good answer as to why it's happening.

    You can use the BigDecimal class to round the number but there's no way you can prevent it from happening. (Don't use Math.round)

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How I can convert Native Code to VB 6.0 Code [ 1 Answers ]

I have a decompiled VB 6.0 Project but source code in Native code but How I can convert Native Code to VB 6.0 Code ? Please solve this hot issue...

Java code [ 2 Answers ]

Please give me a simple java code for addition of two numbers here two numbers are accepted From users please give me explanation about each code

Java code [ 2 Answers ]

Please give me a java program for additon of two numbers here nos are accepted from users here I am not confident about java program so please gieve me java code I only understand a hello program so please help me

Calling Java Script Function in C# code [ 1 Answers ]

How to call a java script function in c# code.

Java code [ 1 Answers ]

What is difference between argument and parameter element as use in calling routines/coding a method? Can one use them interchangeably for talking/writing purposes?


View more questions Search