Skip to content

Commit

Permalink
Fix BigRational#format (#14525)
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed May 21, 2024
1 parent 2ac4b3f commit b563cba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/std/big/big_rational_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ describe BigRational do
describe "#inspect" do
it { 123.to_big_r.inspect.should eq("123") }
end

it "#format" do
br(100, 3).format.should eq("100/3")
br(1234567, 890123).format.should eq("1,234,567/890,123")
br(1234567, 890123).format(".", " ").should eq("1 234 567/890 123")
br(1234567, 890123).format(".", " ", group: 2).should eq("1 23 45 67/89 01 23")
br(1234567, 890123).format(",", ".", group: 4).should eq("123.4567/89.0123")
end
end

describe "BigRational Math" do
Expand Down
7 changes: 7 additions & 0 deletions src/big/big_rational.cr
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ struct BigRational < Number
to_s io
end

# :inherit:
def format(io : IO, separator = '.', delimiter = ',', decimal_places : Int? = nil, *, group : Int = 3, only_significant : Bool = false) : Nil
numerator.format(io, separator, delimiter, decimal_places, group: group, only_significant: only_significant)
io << '/'
denominator.format(io, separator, delimiter, decimal_places, group: group, only_significant: only_significant)
end

def clone
self
end
Expand Down

0 comments on commit b563cba

Please sign in to comment.