+ 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?

20th Jun 2021, 12:25 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
8 Answers
+ 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..
20th Jun 2021, 12:30 PM
Jayakrishna 🇮🇳
+ 2
Thank you very much, now I get it!
20th Jun 2021, 12:31 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
+ 2
Dev thank you for your answer, now I understand it better!
20th Jun 2021, 9:23 PM
Sndr Brst🇩🇪
Sndr Brst🇩🇪 - avatar
0
You're welcome... Almost other methods in Math class will return double value, except round() I guess
20th Jun 2021, 12:34 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 yes you are correct. Because as the name suggests round or roundoff ,for sure it's return type is int
20th Jun 2021, 12:49 PM
Atul [Inactive]
0
Use Math.floor
21st Jun 2021, 6:44 PM
Prashant Sagar
Prashant Sagar - avatar