1

I am trying to multiply 1207.87 by 10000 and expecting 12078700.0 but I am getting 1.2078699999999998E7

I understand that its related to precision mistake but how do i rectify this problem

EDIT:-

I simply want to remove the decimal

Saman Gholami
  • 3,416
  • 7
  • 30
  • 71
Edge
  • 722
  • 2
  • 7
  • 21
  • 2
    How about using `Math.round`? – Antoniossss Feb 26 '14 at 12:37
  • Give us some more info on what You want to do with it. – endriu_l Feb 26 '14 at 12:39
  • sorry guys ...small edit ..I simply want to remove the decimal – Edge Feb 26 '14 at 12:40
  • Then edit the question to reflect what you want instead of putting the real question in the comments and make us all waste time. – m0skit0 Feb 26 '14 at 12:45
  • @m0skit0: I had already edited the question. – Edge Feb 26 '14 at 13:30
  • Can you guys please explain the Down votes. Its a perfectly legit question?? – Edge Feb 26 '14 at 13:31
  • 1) You didn't formulate your question correctly 2) You changed the question in the middle of the answers 3) You changed your question in the comments instead of editing the question – m0skit0 Feb 26 '14 at 14:53
  • @m0skit0 : 1. you can check the timestamps on the question and comment. I first update the question and then the comment 2. Changing question in the middle of answers does not even mean anything. Unanswered question is always in the midle of answers. 3. Still if you want to downvote a question due to above reasons i guess you really dont get a reason of downvote (down vote if it will not be helpful for other people) – Edge Feb 26 '14 at 15:04
  • 1) You didn't even edit the question, someone else did it for you. 2) You cannot change your question to a different question, it's considered a bad question, you can check meta. 3) Downvoting this question is probably the best thing to do given its very poor quality and the time we wasted trying to understand what you want, and then you change your question. IMHO this is disrespectful for people trying to help you. So yeah, downvote stays. – m0skit0 Feb 26 '14 at 15:09

8 Answers8

3

This is because of float numbers representation.
You can:

  • round it Math.round(...)
  • or use BigDecimal if you need high precision.

More about What Every Computer Scientist Should Know About Floating-Point Arithmetic.

m-szalik
  • 3,546
  • 1
  • 21
  • 28
  • I don't think this is a valid solution. You won't get 120700.0 but 120700 (one is a decimal number while the other is an integer), and also you will lose all decimals for other operation values, which will solve problem for these values but break it for all other values. – m0skit0 Feb 26 '14 at 12:39
3

try this:

1207.87f * 10000

the result will be:

1.20787E7
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
1

You need to do the CAST:

(int) 1207.87 * 10000
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You cannot rectify this if you use double type. You can if you use BigDecimal.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

Try to use decimal format like:

 DecimalFormat f = new DecimalFormat("#0.00");
 f.format(Double.valueOf(yourDouble));

Hope this can help you!

Lucian Novac
  • 1,255
  • 12
  • 18
0

double are stored in exponental precision in java and many other languages. see IEEE754for more information about float, double and their variant

If you want a integer result, then cast your floating point number, or the result. If you want to just print a floating number in "ordinary notation", then you can use the class NumberFormat

NumberFormat formatter = new DecimalFormat("#0.00");     
System.out.println(formatter.format(4.0));

example taken from here

finally you can use a fixed precision number with the class BigDecimal, but please take care that this is normally not hardware (FPU) accelerated so will slow down a lot your calculation, but will give you fixed error. That kind of number is especially used in simulation where error must be know

Community
  • 1
  • 1
Lesto
  • 2,260
  • 2
  • 19
  • 26
0

You can use formatted output as below:

double ans = 1207.87 * 10000;
System.out.printf("ans: %.0f\n", ans);

Output:

ans: 12078700
Rahul
  • 3,479
  • 3
  • 16
  • 28
-1

Simple way would be this one:

Double j = 1207.87; 
int x = j.intValue(); 
System.out.println(x * 10000);
user987339
  • 10,519
  • 8
  • 40
  • 45