Skip to content

Commit

Permalink
Add default argument to Number#round
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and Martin Verzilli committed Dec 21, 2017
1 parent 9196a0d commit 2d799c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions spec/std/number_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "spec"

describe "Number" do
describe "significant" do
it "10 base " do
it "10 base" do
1234.567.significant(1).should eq(1000)
1234.567.significant(2).should eq(1200)
1234.567.significant(3).should eq(1230)
Expand All @@ -12,13 +12,13 @@ describe "Number" do
1234.567.significant(7).should eq(1234.567)
end

it "2 base " do
it "2 base" do
-1763.116.significant(2, base: 2).should eq(-1536.0)
753.155.significant(3, base: 2).should eq(768.0)
15.159.significant(1, base: 2).should eq(16.0)
end

it "8 base " do
it "8 base" do
-1763.116.significant(2, base: 8).should eq(-1792.0)
753.155.significant(3, base: 8).should eq(752.0)
15.159.significant(1, base: 8).should eq(16.0)
Expand All @@ -31,19 +31,25 @@ describe "Number" do
end

describe "round" do
it "10 base " do
it "rounds to 0 digits with base 10 from default" do
-1763.116.round.should eq(-1763)
753.155.round.should eq(753)
15.151.round.should eq(15)
end

it "10 base" do
-1763.116.round(2).should eq(-1763.12)
753.155.round(2).should eq(753.16)
15.151.round(2).should eq(15.15)
end

it "2 base " do
it "2 base" do
-1763.116.round(2, base: 2).should eq(-1763.0)
753.155.round(2, base: 2).should eq(753.25)
15.159.round(2, base: 2).should eq(15.25)
end

it "8 base " do
it "8 base" do
-1763.116.round(2, base: 8).should eq(-1763.109375)
753.155.round(1, base: 8).should eq(753.125)
15.159.round(0, base: 8).should eq(15.0)
Expand Down
2 changes: 1 addition & 1 deletion src/number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ struct Number
# ```
# -1763.116.round(2) # => -1763.12
# ```
def round(digits, base = 10)
def round(digits = 0, base = 10)
x = self.to_f
if digits < 0
y = base ** (-digits)
Expand Down

0 comments on commit 2d799c7

Please sign in to comment.