Approximity blog home
1 of 1 article InfoSyndicate: full/short

Truncating floats   23 Oct 04
[print link all ]
I needed to cut off floats after n digits (no rounding).
  class Float
    def truncate(sd=2)
      scale=(10**sd).to_f
      (self * scale).to_i / scale
    end
  end
  a=0.255
  a.truncate(2) #-> 0.25

 

Powered by Rublog