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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: decimal with r_digits 1 always finishes in 0 #2048

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
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