This question already has answers here:
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
Closed 8 years ago.
So I'm making a cookie clicker clone in java (shame on me) and for one thing, I need the double to increase by .1 for each cursor you own. The code looks like this (cookies is a double, and all other values are ints):
cookies = cookies + (cursors*.1+grandmas/2+farms*4+factories*10);
however, when this outputs, it outputs numbers like 2.399999999999999 instead of 2.4. What is going on?
That is double's precision. You could use
cookies = Math.round( cookies * 100.0 ) / 100.0;
Related
This question already has answers here:
How to Java String.format with a variable precision?
(4 answers)
Java printf using variable field size?
(4 answers)
Closed 2 years ago.
I'm a new programmer, and I'm coding a basic decimal calculator. (in Java)
If the user inputs how many decimals they want to round to, say 2, how would I incorporate that into the code, and have the answer round to 2 decimals?
Right now, I have something like this:
System.out.printf("%.3f %n", ans);
But that will always round to 3 decimals, no matter what the user input is.
Here's a link to the whole code, if necessary.
Is there a basic way to this?
This question already has answers here:
Best method to round up to the nearest 0.05 in java
(4 answers)
Closed 4 years ago.
for example the program should be able to execute this task.
21.34 should be displayed as 21.35,
21.32 should be displayed as 21.30,
21.36 should be displayed as 21.35,
21.38 should be displayed as 21.40
please explain or give me code samples on how to make this work. or is it even possible? Thanks!
If you want to round a double to the nearest multiple of 0.05 you can do
d = Math.round(d * 20) / 20.0;
In countries like Australia you need to round to a multiple of 5c.
please explain
As you are asking the question, you really need to be able to explain it.
This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing simple calculation in java. Expected result is 51.3348 but what I am getting is 51.0, here is my calculation
float percent = (7819140000l-3805200000l)*100/7819140000l;
Is that problem with datatype? How can I resolve this to get value as 51.3348
Thanks in Advance
add an f to one of the values:
float percent = (7819140000l-3805200000l)*100f/7819140000l;
if yiu do not do it, Java will make a devision by long values
This question already has answers here:
How can I handle precision error with float in Java?
(9 answers)
Closed 8 years ago.
some calculations with doubles return the wrong result.
E.g.
System.out.println(""+(0.05+0.01));
output
0.060000000000000005
What can I do to correct this error?
Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.
This question already has answers here:
Calculation problem in Java
(2 answers)
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
How can I handle precision error with float in Java?
(9 answers)
Closed 9 years ago.
I don't know whether this question is asked before. I couldn't find it in searches.
System.out.println(1.00 - 9*.10);
the above statement prints out 0.09999999999999998 instead of simply 0.01
I know this is a well known problem in JAVA but i want to know the reason why it is so. Can someone guide me to the implementation details of float and explain the reason.
It's not a problem with java, it's a fundamental problem of the way floating point and double precision numbers are stored and processed in pretty much every language and on every processor.
Because of the way it stores the number it cannot (except in very rare cases) store the number precisely.
A full description of why can be found here: http://en.wikipedia.org/wiki/Single-precision_floating-point_format
You can use things such as BigDecimal to store any precision of number without this issue, however it will run much slower so you have a trade-off of precision vs performance.