You can monkey patch Float to do this, basically. Whether or not it's
a good idea is another matter entirely.
class Float
def to_s
#force all Floats to 2 decimal places
"%.2f" % self
end
end
puts 1.2345678 # => 1.23
Or maybe:
class Float
attr_accessor :decimals
alias_method :default_to_s, :to_s
def to_s
decimals ? "%.#{decimals}f" % self : default_to_s
end
end
a = 1.234567
puts a # => 1.234567
a.decimals = 2
puts a # => 1.23