How to round off prices using Java [duplicate] - java

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.

Related

Round answer based on what the user inputs? [duplicate]

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?

How to round to 1 decimal place using Java [duplicate]

This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.

Exponent Math in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.

Java doesn't know how to multiply? What? [duplicate]

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;

Inconsistent value in floating point operations JAVA [duplicate]

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.

Categories

Resources