Why do my decimal division results return zero mid-call?

● ARCHIVED · READ-ONLY
Started by Arsist 6 posts View original ↗
  1. $game_variables[2] = ((131 / (56 * 12)) * 1000).to_ior

    $game_variables[2] = ((131 / (56 * 12)) * 1000)becomes zero but

    $game_variables[2] = (131000 / (56 * 12))becomes 194.

    What's up with that? How do I work past that?
  2. If you don't specifically tell the program you are working with floats (decimals), it treats everything as integers and cuts off any remainder. Try changing the first value to 131.0 and wrapping the entire thing in (equation).to_i to convert it back to an intergar at the end.

    Edit: Though you will probably want to round it before converting it to an int. .round or something lile that.
  3. that's because the default calculations are always integers and always cut off unless specifically changed to float.


    You either have to make the divisions last (that will only drop the last decimal) or you have to use float numbers in your calculations (131.00 instead of 131) - the first solution is more practical in my opinion because with floats, the engine only changes to float values after the first use of a float, and you can get type errors if you go out of sequence.
  4. So if I don't know if something will turn into a whole number or not, can I multiply it by 1.0 to tell the game I'm working with more than just decimals?

    And go out of sequence?
  5. Arsist said:
    So if I don't know if something will turn into a whole number or not,
    You do know that - because unless you specifically command otherwise, everything will turn into whole integer numbers.
  6. I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


    Yep, variables are integers unless you explicitly tell it otherwise. And since it follows the normal order of mathematical operations, the first point you introduce that .0 is when it will start treating them as floats.


    Your first formula will work if you just change 131 to 131.0