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

Add cardinality for numbers with two decimals #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions lib/brazilian_cardinality/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,41 @@ module Number
class << self
def number_cardinal(number)
negative = number.negative? ? 'menos ' : ''
n = number.to_i.abs

expression = case n
when 0..999 then cardinal_for_0_up_to_999(n)
when 1000..999_999_999_999_999 then cardinal_for_thousands_to_trillions(n)
else raise NumberTooBigError, "#{n} is too big"
end
abs_value = number.abs
decimals = abs_value.to_s.split('.')[1].to_s[0, 2]
integer = abs_value.to_i
expression = raw_cardinal_expression(integer, decimals)

"#{negative}#{expression}"
end

private

def raw_cardinal_expression(integer, decimals)
integer_expression = cardinal_number_integer_part(integer)
cents_expression = cardinal_decimals(decimals)
[integer_expression, cents_expression].compact.join(' vírgula ')
end

def cardinal_number_integer_part(value)
case value
when 0..999 then cardinal_for_0_up_to_999(value)
when 1000..999_999_999_999_999 then cardinal_for_thousands_to_trillions(value)
else raise NumberTooBigError, "#{value} is too big"
end
end

def cardinal_decimals(decimals)
return if decimals.to_i.zero?

if decimals[0].to_i.zero?
decimal_prefix = 'zero'
end

decimals = decimals.to_i
[decimal_prefix, Number.number_cardinal(decimals)].compact.join(' ')
end

def cardinal_for_0_up_to_999(number)
case number
when 0..9 then ONES[number]
Expand Down Expand Up @@ -102,6 +124,20 @@ def cardinal_for_scale_of_thousands(number, scale, singular, plural)
return high_order_units if remainder.zero?
"#{high_order_units} e #{number_cardinal(remainder)}"
end

def cardinal_for_fraction(fraction)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esse método está sendo utilizado em algum lugar? 🤔

return '' unless fraction

fraction = (fraction * 10).to_i

fraction_expression = case fraction
when 0..999 then cardinal_for_0_up_to_999(fraction)
when 1000..999_999_999_999_999 then cardinal_for_thousands_to_trillions(fraction)
else raise NumberTooBigError, "#{fraction} is too big"
end

' vírgula ' + fraction_expression
end
end
end
end
16 changes: 16 additions & 0 deletions test/brazilian_cardinality/number_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,20 @@ def test_negative_trillions
def test_when_number_has_a_scale_bigger_than_trillion
assert_raises(BrazilianCardinality::Number::NumberTooBigError) { @klass.number_cardinal(1_000_000_000_000_000) }
end

def test_number_with_decimals
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acho que seria interessante incluir pelo menos um teste que contemple mais de um zero após a vírgula.

assert_equal 'zero vírgula zero um', @klass.number_cardinal(0.01)
assert_equal 'zero vírgula cinco', @klass.number_cardinal(0.5)
assert_equal 'zero vírgula um', @klass.number_cardinal(0.10)
assert_equal 'zero vírgula onze', @klass.number_cardinal(0.11)
assert_equal 'zero vírgula vinte e três', @klass.number_cardinal(0.23)
assert_equal 'zero vírgula cinquenta e dois', @klass.number_cardinal(0.52)
assert_equal 'um vírgula zero um', @klass.number_cardinal(1.01)
assert_equal 'um', @klass.number_cardinal(1.00)
assert_equal 'um vírgula cinco', @klass.number_cardinal(1.5)
assert_equal 'um vírgula um', @klass.number_cardinal(1.10)
assert_equal 'um vírgula onze', @klass.number_cardinal(1.11)
assert_equal 'um vírgula vinte e três', @klass.number_cardinal(1.23)
assert_equal 'um vírgula cinquenta e dois', @klass.number_cardinal(1.52)
end
end