Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Decimals with variables

During my battles I count the number of actions the user takes, I then divide that by the time it took to complete the battle. However, when I print them out with battle_result, I get rounded numbers with not decimals. How would I get decimals?
x = 4
cx = contents.text_size("Time(" + $game_variables[36].to_s + ") / Act(" + $game_variables[37].to_s + ") =").width
self.contents.draw_text(x, 32, cx, 32, "Time(" + $game_variables[36].to_s + ") / Act(" + $game_variables[37].to_s + ") =")
style_result = $game_variables[36] / $game_variables[37]
cx = contents.text_size("Style(" + style_result.to_s + ")").width
self.contents.draw_text(x, 64, cx, 32, "Style(" + style_result.to_s + ")")
 
Hmm.. maybe this will work.
Code:
x = 4
cx = contents.text_size("Time(" + $game_variables[36].to_s + ") / Act(" + $game_variables[37].to_s + ") =").width
self.contents.draw_text(x, 32, cx, 32, "Time(" + $game_variables[36].to_s + ") / Act(" + $game_variables[37].to_s + ") =")
temp_variable_36 = $game_variables[36] * 100
style_result = temp_variable_36 / $game_variables[37]
style_result *= 0.01
cx = contents.text_size("Style(" + style_result.to_s + ")").width
self.contents.draw_text(x, 64, cx, 32, "Style(" + style_result.to_s + ")")

That will probably round it to the nearest hundredth. if you want to round to the nearest tenth, replace 100 with 10 and 0.01 with 0.1 and if you want it rounded to the nearest thousandth, replace 100 with 1000 and 0.01 with 0.001 etc..
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top