Hello to all jmonkey users,
I have one question for you. Is there a way to round float numbers?
My problem is: Money which I have in game is float value and it’s showing numbers on hundredth (for dummies like me: it’s looking like 5.40). And now if I add 20 cents (0.20f) to wallet, value goes 5.60004. This is what nobody wants. I just want only hundredths, not more zeros.
Code that causes it:
This is Java after all, so there are approximately 2,000 ways to do that.
You could do:
(double)Math.round(money * 100d) / 100d
DecimalFormat d = new DecimalFormat("#.##");
I’m no java expert, so I don’t know which one is best, or which one is fastest. I’d recommend testing a couple thousand runs of each to test for speed.
However, the reason you are seeing the extra 0.00004 in the float, is due to floating point precision. You will see this come up in all forms of computer stuff and if you don’t know whats going on, it’s going to be pretty confusing. If you want precision, it may be better to use java’s BigDecimal instead.
What @Darkchaos said is usually the best option. Using a long or simmilar with 100x multiplier and format when needed to output with DecimalFormat.
Here’s what I use (well, approximatelly) to format my bank account label so it keeps the zero places behind the dot at all times as well:
DecimalFormatSymbols sy = new DecimalFormatSymbols(Locale.US); //otherwise you may get a comma in some countries, ruining your gui placement lol
DecimalFormat pts4 = new DecimalFormat("#.######",sy);
String cash = pts4.format(value/10000d);
cash = cash+"."; //keep the decimal places at all times
if(value >= 0)
while(cash.length() < 6)
cash = cash+"0";
else // Negative values have an extra "-" character which we have to account for
while(cash.length() < 7)
cash = cash+"0";
I’ve set it to keep 6 decimals, because cryptocurrency. You’d only need 3.
well, using integers won’t solve that problem. It does however solve a bigger one : Not having to deal with float impressions.
You said that adding 20 cents (0.20f) to a wallet containing 5.40 caused it to contain 5.60004.
That means that the user suddenly has 0.00004 more. This may not look like a problem but it means that every time you want to do something with the money of a player you have a chance to add extra or maybe remove money from him. And though you can round it now, maybe you will eventually get an error that is so big that rounding won’t solve it.
Additionally that means that you need to round it every time you want to do something with it, forget it once and you are in trouble.
To fix all of this you just simply store the money internally as integers. Then only when you want to display the values you need to worry about rounding it and even if there is an error shown due to floats it is only in what the user sees and thus won’t result in an exploit that can result in infinite money.
You have true that work with Integer is better. But I failed in rounding Integer again because dividing Integer by 100 isn’t possible. You actually can make an Integer with two zeros behind point, mult it by * 0.01, but there a lot of inaccuracy are made.