+ 1
How does rounding work? Why is my result 0.0 instead of 0.4?
https://code.sololearn.com/cwr3XmHGgJDc/?ref=app If I put in the input tomato 2 20 the RoundedSaving output is 0.0 even though it needs to be 0.4. Without rounding it was 0.399999, so I tried to round it to 0.4 as I wanted the result only correct to two decimal places. But why is the output for RoundedSaving now 0.0? What can I do to round correctly to two decimal places?
6 Réponses
+ 2
Katharina Hohenfels
Use
double savingRounded = Math.round(saving * 100) / 100.0;
Math.round() returns integer so int/int results int only.. you need atleast a double value like double/int or int/double..
+ 2
Thank you very much, now I get it!
+ 2
Dev thank you for your answer, now I understand it better!
0
You're welcome...
Almost other methods in Math class will return double value, except round() I guess
0
Jayakrishna🇮🇳 yes you are correct. Because as the name suggests round or roundoff ,for sure it's return type is int
0
Use Math.floor