Names to numbers: Difference between revisions

Content added Content deleted
No edit summary
Line 763: Line 763:
This solution uses "Number names" from [[Number_names#Ruby | here]]
This solution uses "Number names" from [[Number_names#Ruby | here]]
{{trans|Python}}
{{trans|Python}}
<lang ruby>require 'Number_names'
<lang ruby>require 'number_names'


def int_from_words(num)
def int_from_words(num)
words = num.downcase.gsub(/(,| and |-)/,' ').split
words = num.downcase.gsub(/(,| and |-)/,' ').split
if words[0] =~ /(minus|negative)/ # or words[0] == 'negative'
if words[0] =~ /(minus|negative)/
negmult = -1
negmult = -1
words.shift
words.shift
Line 793: Line 793:
end
end
negmult * (total + small)
negmult * (total + small)
end
end</lang>


Examples:
# examples
for n in (-10000..10000).step(17)
<lang ruby>for n in (-10000..10000).step(17)
raise unless n == int_from_words(wordify(n))
raise unless n == int_from_words(wordify(n))
end
end
Line 823: Line 823:
{{out}}
{{out}}
<pre>
<pre>
##
## These tests show <==> for a successful round trip, otherwise <??>
##
+0 <==> zero
-3 <==> negative three
+5 <==> five
-7 <==> negative seven
+11 <==> eleven
-13 <==> negative thirteen
+17 <==> seventeen
-19 <==> negative nineteen
+23 <==> twenty-three
-29 <==> negative twenty-nine

201021002001 <==> two hundred and one billion, twenty-one million, two thousand, one
-20102100201 <==> negative twenty billion, one hundred and two million, one hundred thousand, two hundred and one
2010210020 <==> two billion, ten million, two hundred and ten thousand, twenty
-201021002 <==> negative two hundred and one million, twenty-one thousand, two
20102100 <==> twenty million, one hundred and two thousand, one hundred
-2010210 <==> negative two million, ten thousand, two hundred and ten
201021 <==> two hundred and one thousand, twenty-one
-20103 <==> negative twenty thousand, one hundred and three
2010 <==> two thousand, ten
-201 <==> negative two hundred and one
20 <==> twenty
-2 <==> negative two
0 <==> zero
</pre>
</pre>