title: How do I control the decimals in my float calculations?
body: <p>I'm trying to run a few calculations in order to represent a particular price (ie 20.30).</p>
  
  <p>I have tried the Float#round method, but the instance variables holding these values eventually start representing numbers that look like 24.43418 after a few calculations.</p>
  
  <p>This is just a method I created to turn a users input into a percentage</p>
  
  <pre><code>class Fixnum
   def percentage
    self.to_f / 100
   end
  end
  </code></pre>
  
  <p>The @bankroll_amount and @risk_amount values should be evaluating to two decimal points</p>
  
  <pre><code>class Client
  
   def initialize(bankroll, unit)
    @bankroll_amount = bankroll.to_i.round(2)
    @unit_percentage = unit.to_i.percentage
    default_risk_amount.round(2)
    evaluate_default_unit!.round(2)
   end
  
   def default_risk_amount
    @risk_amount = @unit_percentage * @bankroll_amount
    @risk_amount.round(2)
   end
  
   # simulates what an updated bankroll looks like after a win based on clients "unit"  amount
   def risk_win
    @bankroll_amount = @bankroll_amount + @risk_amount
    @bankroll_amount.round(2)
    evaluate_default_unit!.round(2)
   end
  
   # simulates what a clients updated bankroll looks like after a loss based on clients "unit" amount
   def risk_loss
    @bankroll_amount = @bankroll_amount - @risk_amount
    evaluate_default_unit!
   end
  
  
   def evaluate_default_unit!
    @risk_amount = @unit_percentage * @bankroll_amount.round(2)
   end
  
  end
  </code></pre>
  
  <p>Im not sure if this has anything to do with the fact that I am initializing these instance variables or not, but the @risk_amount returns the correct two decimal value, but when I return the object, the instance variable inside has running decimals.</p>
  
  <pre><code>c = Client.new 2000, 1
  &lt;Client:0x000001018956a0 @bankroll_amount=2000.0, @unit_percentage=0.01, @risk_amount=20.0&gt;
  
  c.risk_win
  =&gt; 20.2
  </code></pre>
  
  <p>When I run c.risk_win enough, it eventually returns</p>
  
  <pre><code>c
  &lt;Client:0x000001018956a0 @bankroll_amount=2440.3802, @unit_percentage=0.01, @risk_amount=24.4038&gt;
  </code></pre>