+ 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..
4 Réponses
+ 9
I inspected your code
do it through double casting
return overs = (double) ((int) (overs*10))/10;
+ 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.
+ 7
Thanks
+ 5
thanks,but there should be an easy way out..