Skip to content

Instantly share code, notes, and snippets.

@prathe
Created January 17, 2012 20:51
Show Gist options
  • Save prathe/1628799 to your computer and use it in GitHub Desktop.
Save prathe/1628799 to your computer and use it in GitHub Desktop.
BigDecimal#to_s without trailing zero
class BigDecimal
def to_s(s = 'F')
sign, significant_digits, base, exponent = self.split
if self.zero?
'0'
elsif significant_digits.size <= exponent
self.to_i.to_s
else
self.to_f.to_s
end
end
end
@jgarber
Copy link

jgarber commented Oct 4, 2012

Probably not what you wanted...

irb(main):002:0> b = BigDecimal.new("9.0000000000000009")
=> #<BigDecimal:2ae7455445f0,'0.9000000000 0000009E1',27(27)>
irb(main):003:0> b.to_dig
=> "9.0"

Also probably shouldn't duck punch to_s. See my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment