+ 7

Truncate

I made a cricket game in which I have overs , which are displayed as 0.30000000004 sometimes... I want :- 0.3 How do I truncate these values? Code :- https://code.sololearn.com/c56Bnye5KD1E/?ref=app Scroll down to getOvers method, You can find it by reading comments, that comment starts with convert. Here, I returned overs variable, type : double. It sometimes returns 1.2 etc , which is correct, but sometimes it returns 0.300000004 etc, which I need to get truncated. Thanks..

8th May 2017, 5:43 AM
Meharban Singh
Meharban Singh - avatar
4 Réponses
+ 9
I inspected your code do it through double casting return overs = (double) ((int) (overs*10))/10;
8th May 2017, 11:18 AM
Alexey
Alexey - avatar
+ 8
Try to use BigDecimal wrapper instead double. To determine the number of digits, there is a method: int scale(). To specify the requared number of digits: setScale(int, roundingMode) Note that BigDecimal objects are immutable.
8th May 2017, 6:59 AM
Alexey
Alexey - avatar
+ 7
Thanks
8th May 2017, 11:26 AM
Meharban Singh
Meharban Singh - avatar
+ 5
thanks,but there should be an easy way out..
8th May 2017, 9:10 AM
Meharban Singh
Meharban Singh - avatar