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.

[Resolved]-Cutting back a decimal value

Thank you both for helping me, both methods work pretty well!

darkleo's method works for having a value between .0 and .00000000000000, just change the +m to how many decimals you want left over, but sometimes it'll cut it off to just 1 decimal digit when using thousand+ valued numbers.

Code:
class Game_Variables
  #--------------------------------------------------------------------------
  # * Set Variable
  #     variable_id : variable ID
  #     value       : the variable's value
  #--------------------------------------------------------------------------
  def []=(variable_id, value)
    if variable_id <= 5000
      if value.is_a?(Float)
        @value = value
        @decimals = @value.to_i.size + 1
        value = @value.to_s[0, 2+@decimals].to_f
      end
      @data[variable_id] = value
    end
  end
end

kmhp's method however will always have the specified decimal ammount, as long as the last values aren't 0!

Code:
class Game_Variables
  #--------------------------------------------------------------------------
  # * Set Variable
  #     variable_id : variable ID
  #     value       : the variable's value
  #--------------------------------------------------------------------------
  def []=(variable_id, value)
    if variable_id <= 5000
      if value.is_a?(Float)
        value = sprintf('%.2f', value).to_f 
      end
      @data[variable_id] = value
    end
  end
end

I shall now label this topic as "Resolved"  :thumb:

How do you cut back a decimal value? For instance, when tracking Player's Screen Y, sometimes the value will show 260.35745572234523457.

How do I cut it so it only shows, like 260... or 260.3... or 260.35?

I searched the help file and tried screen_x.truncate, but it didn't do anything but error pop for me.
 
Code:
  n = 3.1415926535897932384626433832795028841971
  m = n.to_i.to_s.size + 1
  n = n.to_s[0, 5+m].to_f
  p n
n is PI. (yeah !)
The last line return PI with 5 numbers after the "."
 
Definately a functional method, isn't there a simpler one though? Reguardless, this works just fine for what I needed it for, its just kinda confusing.

Everythings working great now, thank you for helping me out! :thumb:
 

khmp

Sponsor

You could try something like this.
Code:
float_number = 5.323423425
p sprintf('%.2f', float_number).to_f # 5.32
float_number = 5.3
p sprintf('%.2f', float_number).to_f # 5.3

Good luck with it Kain Nobel! :thumb:
 

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