mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Patching awesome_bigdecimal to not have lossy precision.
This commit is contained in:
parent
fea9567eb4
commit
bd032ca03b
4 changed files with 16 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
|||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
awesome_print (1.0.0)
|
||||
awesome_print (1.0.2)
|
||||
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
|
|
|
@ -189,12 +189,17 @@ module AwesomePrint
|
|||
colorize(ls.empty? ? d.inspect : "#{d.inspect}\n#{ls.chop}", :dir)
|
||||
end
|
||||
|
||||
# Format BigDecimal and Rational objects by convering them to Float.
|
||||
# Format BigDecimal object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_bigdecimal(n)
|
||||
colorize(n.to_f.inspect, :bigdecimal)
|
||||
colorize(n.to_s("F"), :bigdecimal)
|
||||
end
|
||||
|
||||
# Format Rational object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_rational(n)
|
||||
colorize(n.to_s, :rational)
|
||||
end
|
||||
alias :awesome_rational :awesome_bigdecimal
|
||||
|
||||
# Format a method.
|
||||
#------------------------------------------------------------------------------
|
||||
|
|
|
@ -43,6 +43,7 @@ module AwesomePrint
|
|||
:keyword => :cyan,
|
||||
:method => :purpleish,
|
||||
:nilclass => :red,
|
||||
:rational => :blue,
|
||||
:string => :yellowish,
|
||||
:struct => :pale,
|
||||
:symbol => :cyanish,
|
||||
|
|
|
@ -479,14 +479,14 @@ EOS
|
|||
|
||||
#------------------------------------------------------------------------------
|
||||
describe "BigDecimal and Rational" do
|
||||
it "should present BigDecimal object as Float scalar" do
|
||||
big = BigDecimal("2010.4")
|
||||
big.ai(:plain => true).should == "2010.4"
|
||||
it "should present BigDecimal object with arbitrary precision" do
|
||||
big = BigDecimal("201020102010201020102010201020102010.4")
|
||||
big.ai(:plain => true).should == "201020102010201020102010201020102010.4"
|
||||
end
|
||||
|
||||
it "should present Rational object as Float scalar" do
|
||||
rat = Rational(2010, 2)
|
||||
rat.ai(:plain => true).should == "1005.0"
|
||||
it "should present Rational object with arbitrary precision" do
|
||||
rat = Rational(201020102010201020102010201020102010, 2)
|
||||
rat.ai(:plain => true).should == "100510051005100510051005100510051005/1"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue