Global Variables dont get proper values if any operation is done on them with values that are not whole numbers

Started by LostInThought12 on

Topic category: Help with modding (Java Edition)

Last seen on 04:08, 17. May 2024
Joined May 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Global Variables dont get proper values if any operation is done on them with values that are not whole numbers

Was trying to make a GUI Level-Up system yesterday that allows you to change your stats when I noticed that the global variables that I  had set to add/subtract values with numbers that are not whole number's started giving wrong results after a few operations. For example a number that is supposed to increment by 0.5 every button press gave me values like 6.000004 and on subtraction went down to E+ values and below 0 even when  I set an if condition to not decrease value if its already 0. 

This is normal behavior…
Thu, 05/16/2024 - 18:40

This is normal behavior because Java rounds decimal numbers unpredictably. If you want to use 100% accurate numbers, use whole numbers, or format number when printing to 1-2 decimal places