Skip to content

Commit

Permalink
Fix: decimal with r_digits 1 always finishes in 0 (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaimem committed Jun 11, 2020
1 parent 77a39a0 commit 6f8b25a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions lib/faker/default/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ def decimal(legacy_l_digits = NOT_GIVEN, legacy_r_digits = NOT_GIVEN, l_digits:
end

l_d = number(digits: l_digits)
r_d = if r_digits == 1
generate(r_digits)
else
# Ensure the last digit is not zero
# so it does not get truncated on converting to float
generate(r_digits - 1).join + non_zero_digit.to_s
end

# Ensure the last digit is not zero
# so it does not get truncated on converting to float
r_d = generate(r_digits - 1).join + non_zero_digit.to_s

"#{l_d}.#{r_d}".to_f
end

Expand Down
6 changes: 3 additions & 3 deletions test/faker/default/test_faker_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_number_with_one_digit
end

def test_decimal
assert @tester.decimal(l_digits: 1, r_digits: 1).to_s.match(/[0-9]{1}\.[0-9]{1}/)
assert @tester.decimal(l_digits: 2).to_s.match(/[0-9]{2}\.[0-9]{2}/)
assert @tester.decimal(l_digits: 4, r_digits: 5).to_s.match(/[0-9]{4}\.[0-9]{5}/)
assert @tester.decimal(l_digits: 1, r_digits: 1).to_s.match(/[0-9]{1}\.[1-9]{1}/)
assert @tester.decimal(l_digits: 2).to_s.match(/[0-9]{2}\.[0-9]{1}[1-9]{1}/)
assert @tester.decimal(l_digits: 4, r_digits: 5).to_s.match(/[0-9]{4}\.[0-9]{4}[1-9]{1}/)
end

def test_digit
Expand Down

0 comments on commit 6f8b25a

Please sign in to comment.