Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigRational#format #14525

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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