#DOUBLE value not adding correctly (e.g. 0.40 becomes 0.39999999)

1 messages · Page 1 of 1 (latest)

dense matrix
#

Hi! I'm currently using the meta option in DeluxeMenus to store and add values like this:

- '[meta] add customBoxKosztTotal DOUBLE 0.40'

The problem is that when I start from a number like 28.5 and add 0.40, the result becomes something like 28.89999999999998 instead of 28.9. It seems like there's a floating-point precision issue with how DOUBLE values are being added.

Is there any fix or workaround for this? I really need this to work properly as soon as possible.

cold apex
#

this is fundamentally how doubles and all floating points work in all of computing

#

what is the use case here to need to be added with such precision?

dense matrix
#

I need precise calculations because I’m working on a "Wallet" system, and unfortunately, if a placeholder returns a value like 43.29999999999998, it looks messy and causes me to subtract an inaccurate amount from the player’s account. Is there any alternative I could use or a way to fix this issue? Something like that?

cold apex
#

from your end i dont think so, i was thinking rounding within the math expansion, but this would cause further inaccuracies

#

@eager coyote i dont think its worth adding a BigDecimal data type to meta? :/

#

seems a bit too much

dense matrix
#

I simply need it to return 9.99 instead of 9.9999999999998, so that the command executed by the console can subtract the full amount for the product, instead of an inaccurate one
I struggled a bit with this wallet because I'm not that familiar with this topic, but it was only with the "custom box" product that this problem appeared :/

#

I don't have this problem with adding and subtracting 0.5, 1.0, but when I add or subtract the number 0.4, 0.8 this problem appears

dense matrix
eager coyote
dense matrix
#

I forgot