0

Suppose n is a Double type variable .

double right=n-(Math.ceil(n)-1);

//Here I am trying to get the right part of the number.

Now if n=1234.78

then right=0.7799999999999727

why not .78?

and when

n=1234.89

then right=0.8900000000001

why not 89? and why not 9999... here instead of 000000....?

Now suppose I want to find the sum of digits in right..Like in my example for 1234.89 its 8+9=17 or 1234.781 its 7+8+1-16. So then ?what should I do?

But I can't do it using floating point arithmatic?Like

double temp=0.0; 
while(right>0) 
{ 
right=rigth*10; 
temp=Math.floor(right); 
right=right-temp; 
suml+=temp; 
} 

in kind of a way I mentioned above? I am new to java. please explain my problem. It would be a great help for me. Thank you.

  • 1
    Floating point numbers are not 100% accurate, i.e. `0.1` can't be represented exactly. If you need 100% accuracy you can use `BigInteger` or `int` – jlordo Dec 14 '12 at 10:40
  • 2
    Possible duplicate : http://stackoverflow.com/questions/177506/why-do-i-see-a-double-variable-initialized-to-some-value-like-21-4-as-21-3999996 – xlecoustillier Dec 14 '12 at 10:40
  • In addition to what everyone else has said, if you are just interested in the decimal part: double right = n % 1; is in my opinion simpler way. – 1615903 Dec 14 '12 at 10:42
  • Possible dublicate:http://stackoverflow.com/questions/322749/retain-precision-with-doubles-in-java – bNd Dec 14 '12 at 10:52

2 Answers2

0

Hey you can use the following code

    double roundTwoDecimals(double d) {
                DecimalFormat twoDForm = new DecimalFormat("#.##");
            return Double.valueOf(twoDForm.format(d));
    }
right=roundTwoDecimals(right);
    System.out.println(right);

You will get desired result.

onkar
  • 4,427
  • 10
  • 52
  • 89
  • Now suppose I want to find the sum of digits in right..Like in my example for 1234.89 its 8+9=17 or 1234.781 its 7+8+1-16. So then ?what should I do? – Arijit Saha Dec 14 '12 at 10:49
  • What is your exact requirement ? Getting sum of decimals after right or correcting it to 2 decimals. – onkar Dec 14 '12 at 10:51
  • Get substring from . to end. Convert that String to integer, iterate it into loop and add the values. – onkar Dec 14 '12 at 10:56
  • Thank you..But I can't do it using floating point arithmatic?Like double temp=0.0; while(right>0) { right=rigth*10; temp=Math.floor(right); right=right-temp; suml+=temp; } in kind of a way I mentioned above? – Arijit Saha Dec 14 '12 at 11:00
  • @ArijitSaha I suppose that approach should work.Please share your errors/expected and output obtained – onkar Dec 14 '12 at 13:35
0

floating point works the same in almost all languages and you see the same issues. If you want the fractional part you should use.

double fract = n - (long) n;

I suspect the formula you are using will not work for negative numbers. (and is slower)

This can produce a rounding error as you have seen so you need to chose a level of precision. You might choose 2 or 6 digits. You can round the value when you calculate it but it's often simpler to round it when you print it.

System.out.printf("%.2f%n", fract);

This will print 0.78 and 0.89 as you expect.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130