Talk:Number names: Difference between revisions

m
(PARI/GP code doesn't work.)
m (→‎PARI/GP Code Doesn't Work: Fixed formatting.)
 
(3 intermediate revisions by the same user not shown)
Line 64:
};
\\ Original code above here. Below is my test code.
 
test_nums = [1, 23, 456, 7890, 12345, 678901, 2345678];
Line 77 ⟶ 78:
678901 = nine hundred one
2345678 = six hundred seventy-eight</lang>
 
Follow-up: The code works for 1 thru 1099, but produces incorrect output at 1100:
 
<lang parigp>1 = one
...
1099 = one thousand ninety-nine
1100 = one hundred</lang>
 
--[[User:Chuck Coker|Chuck Coker]] ([[User talk:Chuck Coker|talk]]) 09:02, 23 June 2019 (UTC)
 
:I figured out the problem. In the millions, thousands, and hundreds sections, the lines:
 
:<lang parigp> s = Str(Eng(tmp), " million");
...
s = Str(Eng(tmp), " thousand");
...
s = Str(Edigit(tmp), " hundred");</lang>
 
: need to be changed to:
 
:<lang parigp> s = Str(s, Eng(tmp), " million");
...
s = Str(s, Eng(tmp), " thousand");
...
s = Str(s, Edigit(tmp), " hundred");</lang>
 
: The string s was being overwritten at each step until you get down into the hundreds section.
 
:The final code should look like this:
 
:<lang parigp>Eng(n:int)={
my(tmp, s="");
if (n >= 1000000,
tmp = n\1000000;
s = Str(s, Eng(tmp), " million");
n -= tmp * 1000000;
if (!n, return(s));
s = Str(s, " ")
);
if (n >= 1000,
tmp = n\1000;
s = Str(s, Eng(tmp), " thousand");
n -= tmp * 1000;
if (!n, return(s));
s = Str(s, " ")
);
if (n >= 100,
tmp = n\100;
s = Str(s, Edigit(tmp), " hundred");
n -= tmp * 100;
if (!n, return(s));
s = Str(s, " ")
);
if (n < 20,
return (Str(s, ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"][n]))
);
tmp = n\10;
s = Str(s, [0, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"][tmp]);
n -= tmp * 10;
if (n, Str(s, "-", Edigit(n)), s)
};
Edigit(n)={
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][n]
};
test_nums = [1, 23, 456, 7890, 12345, 678901, 2345678, 90123456, 789012345, 999999999, 1000000000, 6789012345, 67890123456, 789012345678, 8901234567890];
for (x = 1, #test_nums, print(test_nums[x] " = " Eng(test_nums[x])));</lang>
 
: The final output works up to 999,999,999. (I haven't checked ''all'' the numbers, but spot checks seem to work.) Numbers higher than that fail.
 
:<lang parigp>1 = one
23 = twenty-three
456 = four hundred fifty-six
7890 = seven thousand eight hundred ninety
12345 = twelve thousand three hundred forty-five
678901 = six hundred seventy-eight thousand nine hundred one
2345678 = two million three hundred forty-five thousand six hundred seventy-eight
90123456 = ninety million one hundred twenty-three thousand four hundred fifty-six
789012345 = seven hundred eighty-nine million twelve thousand three hundred forty-five
999999999 = nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine
1000000000 = one thousand million
6789012345 = six thousand seven hundred eighty-nine million twelve thousand three hundred forty-five
67890123456 = sixty-seven thousand eight hundred ninety million one hundred twenty-three thousand four hundred fifty-six
789012345678 = seven hundred eighty-nine thousand twelve million three hundred forty-five thousand six hundred seventy-eight
8901234567890 = eight million nine hundred one thousand two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety</lang>
 
: Of course, once you get this far, it's easy enough to extend the range of numbers handled. For example, to add billions and trillions, change the following lines:
 
:<lang parigp>Eng(n:int)={
my(tmp, s="");
if (n >= 1000000,
...</lang>
 
: to:
 
:<lang parigp>Eng(n:int)={
my(tmp, s="");
if (n >= 1000000000000,
tmp = n\1000000000000;
s = Str(s, Eng(tmp), " trillion");
n -= tmp * 1000000000000;
if (!n, return(s));
s = Str(s, " ")
);
if (n >= 1000000000,
tmp = n\1000000000;
s = Str(s, Eng(tmp), " billion");
n -= tmp * 1000000000;
if (!n, return(s));
s = Str(s, " ")
);
if (n >= 1000000,
...</lang>
 
: Ouput:
 
:<lang parigp>1 = one
23 = twenty-three
456 = four hundred fifty-six
7890 = seven thousand eight hundred ninety
12345 = twelve thousand three hundred forty-five
678901 = six hundred seventy-eight thousand nine hundred one
2345678 = two million three hundred forty-five thousand six hundred seventy-eight
90123456 = ninety million one hundred twenty-three thousand four hundred fifty-six
789012345 = seven hundred eighty-nine million twelve thousand three hundred forty-five
999999999 = nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine
1000000000 = one billion
6789012345 = six billion seven hundred eighty-nine million twelve thousand three hundred forty-five
67890123456 = sixty-seven billion eight hundred ninety million one hundred twenty-three thousand four hundred fifty-six
789012345678 = seven hundred eighty-nine billion twelve million three hundred forty-five thousand six hundred seventy-eight
8901234567890 = eight trillion nine hundred one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety</lang>
 
:--[[User:Chuck Coker|Chuck Coker]] ([[User talk:Chuck Coker|talk]]) 10:22, 23 June 2019 (UTC)