Search code examples
rubyroundingcurrencylinguistics

How do I preserve my float number in ruby


So I'm trying some code out to convert numbers into strings. However, I noticed that in certain cases it does not preserve the last two decimal places. For instance I type 1.01 and 1.04 for addition and I get back 2.04. If I type just 1.05 it preserves the number and returns it exactly. I get whats going on things are being rounded. I don't know how to prevent it from being rounded though. Should I just consider sending (1.01+1.04) to self as only one input?

Warning! I haven't tried this yet so don't know if its supported:

 user_input = (1.04+1.01) #entry from user
 user_input = gets.to_f
 user_input.to_test_string

What I have so far:

    class Float
     def to_test_string

      cents = self % 1
      dollars = self - cents
      cents = cents * 100

      text = "#{dollars.to_i.en.numwords} dollars and #{cents.to_i.en.numwords} cents"

      puts text
      text
     end
    end
  puts "Enter two great floating point numbers for adding"
  puts "First number"
  c = gets.to_f
  puts "Second number"
  d = gets.to_f
  e = c+d
  puts e.to_test_string
  puts "Enter a great floating number! Example 10.34"
  a = gets.to_f 
  puts a.to_test_string

Thanks for the help! Post some code up so I can try!


Solution

  • First of all: never use float for money — Use Float or Decimal for Accounting Application Dollar Amount?

    irb> x = 1.01 + 1.04
    => 2.05
    irb> y = x % 1
    => 0.04999999999999982
    irb> (y * 100).to_i
    => 4
    

    But if want it VERYVERYVERY much:

    irb> (y * 100).round.to_i
    => 5