Number names: Difference between revisions

Added Easylang
(Didn't like the adaptation of the Applesoft example. Rewrote a new example processing the number input as a string.)
(Added Easylang)
 
(43 intermediate revisions by 22 users not shown)
Line 15:
=={{header|360 Assembly}}==
{{trans|AppleSoft Basic}}
<langsyntaxhighlight lang="360asm">* Number names 20/02/2017
NUMNAME CSECT
USING NUMNAME,R13
Line 197:
PG DS CL256
YREGS
END NUMNAME</langsyntaxhighlight>
{{out}}
<pre>
Line 216:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_IO;
 
procedure Integers_In_English is
Line 347:
Spell_And_Print(Samples(I));
end loop;
end Integers_In_English;</langsyntaxhighlight>
The implementation goes up to 10<sup>18</sup>-1
and also supports negative and zero inputs.
Line 376:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8.8d.fc9.i386]}}
<langsyntaxhighlight lang="algol68">PROC number words = (INT n)STRING:(
# returns a string representation of n in words. Currently
deals with anything from 0 to 999 999 999. #
Line 413:
OD;
stop iteration:
SKIP</langsyntaxhighlight>
 
{{out|Example input with output}}
Line 426:
 
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
<langsyntaxhighlight Algol68lang="algol68">MODE EXCEPTION = STRUCT(STRING name, PROC VOID handler);
EXCEPTION value error = ("Value Error", stop);
 
Line 514:
FOR i TO 6 DO prod := prod * 10**i + i; example(prod) OD;
 
example(1278); example(1572); example(2010)</langsyntaxhighlight>
{{out}}
<pre>
Line 527:
2010: two thousand and ten
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">spell←{⎕IO←0
small←'' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight'
small,←'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen'
small,←'fifteen' 'sixteen' 'seventeen' 'eighteen' 'nineteen'
teens←'twenty' 'thirty' 'forty' 'fifty' 'sixty' 'seventy'
teens,←'eighty' 'ninety'
orders←'m' 'b' 'tr' 'quadr',¨⊂'illion'
orders←(⊂¨,¨10*6+3×⍳∘≢)orders
orders←('hundred' 100)('thousand' 1000),orders
 
⍵=0:'zero'
{ ⍵<0:'minus ',∇-⍵
⍵<20:⍵⊃small
⍵<100:{
ty←((⌊⍵÷10)-2)⊃teens
0=n←10|⍵:ty
ty,'-',n⊃small
}⍵
(⊃⊃∇{
name size←⍺
cur n←⍵
rest←size|n
n≥size:(⊂cur,(⍺⍺⌊n÷size),' ',name,((0≠rest)↑'')),rest
(⊂cur),rest
}/orders,⊂(''⍵)),∇100|⍵
}⍵
}</syntaxhighlight>
{{out}}
<pre> spell 0
zero
spell 12345
twelve thousand three hundred forty-five
spell -6789
minus six thousand seven hundred eighty-nine
spell 2*48
two hundred eighty-one trillion four hundred seventy-four billion nine hundred seventy-six million seven hundred ten thousand six hundred fifty-six</pre>
 
=={{header|AppleScript}}==
 
With AppleScript's ability to access some of macOS's Objective-C frameworks, it's possible to get the job done with a single line:
 
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
on numberToWords(n)
return (current application's class "NSNumberFormatter"'s localizedStringFromNumber:(n) numberStyle:(current application's NSNumberFormatterSpellOutStyle)) as text
end numberToWords
 
numberToWords(-3.6028797018963E+10)
--> "minus thirty-six billion twenty-eight million seven hundred ninety-seven thousand eighteen point nine six three"</syntaxhighlight>
 
NSNumberFormatter supports several natural languages and by default uses the one set for the user on the host machine. However, its English is limited to US English, so there are no "and"s in the results.
 
Vanilla AppleScript can be more flexible if you're prepared to write the code. The script below, like the ASObjC one above, returns short-scale number names, but includes "and"s by default. An optional parameter <code>without ands</code> can be added to the call if the "and"s are not wanted. Another optional parameter, <code>with longScale</code>, gets a British-English long-scale result, while a third, <code>with milliards</code>, gets a long-scale result with "milliard", "billiard", ''etc.'' between the "-illion"s instead of "thousand".
 
Both scripts can display strange results with numbers at the extreme limits of floating-point resolution.
 
<syntaxhighlight lang="applescript">-- Parameters:
-- n: AppleScript integer or real.
-- longScale (optional): boolean. Whether to use long-scale -illions instead of short-scale. Default: false
-- milliards (optional): boolean. Whether to use long-scale -illiards instead of long-scale thousands.
-- ands (optional): boolean. Whether to include "and"s in the result. Default: true.
on numberToEnglish from n given longScale:usingLongScale : false, milliards:usingMilliards : false, ands:usingAnd : true
-- If 'with milliards' is specified, make sure the differently coded 'with longScale' is disabled.
if (usingMilliards) then set (usingLongScale) to false
-- Script object containing data and two subhandlers.
script o
property scale : 1000
property unitsAndTeens : {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", ¬
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
property tens : {missing value, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
property landmarks : {"thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion"}
property illiardLandmarks : {"thousand", "million", "milliard", "billion", "billiard", "trillion", "trilliard", ¬
"quadrillion", "quadrilliard", "quintillion", "quintilliard", "sextillion", "sextilliard", "septillion", "septilliard"}
property collector : {} -- Words collected here.
-- Deal with the integer part of the number.
on nameInteger(n, landmarkIndex)
-- Recursively work to the "front" of the number, three or six "digits" at a time depending on the scale.
if (n ≥ scale) then nameInteger(n div scale, landmarkIndex + 1)
-- Name each digit-group value on the way back.
set groupValue to n mod scale
-- If a group value's over 999, its top three digits represent thousands in a long-scale naming. Deal with them first.
if (groupValue > 999) then
nameGroup(groupValue div 1000, false, landmarkIndex)
set end of my collector to "thousand"
-- In this context, if the group's bottom three digits amount to zero, the appropriate "-illion", if any, must be added here.
set groupValue to groupValue mod 1000
if ((groupValue is 0) and (landmarkIndex > 0)) then set end of my collector to item landmarkIndex of my landmarks
end if
-- Deal with either a short-scale digit group or the bottom three digits of a long-scale one.
if (groupValue > 0) then
nameGroup(groupValue, true, landmarkIndex)
if (landmarkIndex > 0) then set end of my collector to item landmarkIndex of my landmarks
end if
end nameInteger
-- Deal with a value representing a group of up to three digits.
on nameGroup(groupValue, notThousands, landmarkIndex)
-- Firstly the hundreds, if any.
if (groupValue > 99) then
set end of my collector to item (groupValue div 100) of unitsAndTeens
set end of my collector to "hundred"
end if
-- Then the tens and units together, according to whether they require single words, hyphenated words or none.
set tensAndUnits to groupValue mod 100
if (tensAndUnits > 0) then
-- Insert the word "and" if enabled and appropriate.
if ((usingAnd) and ¬
((collector ends with "hundred") ¬
or (collector ends with "thousand") ¬
or ((notThousands) and (landmarkIndex is 0) and (collector is not {})))) then ¬
set end of my collector to "and"
if (tensAndUnits < 20) then
set end of my collector to item tensAndUnits of my unitsAndTeens
else
set units to tensAndUnits mod 10
if (units > 0) then
set end of my collector to item (tensAndUnits div 10) of my tens & ("-" & item units of my unitsAndTeens)
else
set end of my collector to item (tensAndUnits div 10) of my tens
end if
end if
end if
end nameGroup
end script
(* Main handler code. *)
-- Adjust for a negative if necessary.
if (n < 0) then set {end of o's collector, n} to {"minus", -n}
-- Deal with the integer part of the number.
if (n div 1 is 0) then
set end of o's collector to "zero"
else
if (usingLongScale) then
set o's scale to 1000000
set o's landmarks to rest of o's landmarks
else if (usingMilliards) then
set o's landmarks to o's illiardLandmarks
end if
tell o to nameInteger(n div 1, 0)
end if
-- Deal with any fractional part. (Vulnerable to floating-point inaccuracy with extreme values.)
if (n mod 1 > 0.0) then
set end of o's collector to "point"
-- Shift each fractional digit into the units position and read it off as an integer.
set n to n * 10
repeat
set units to n mod 10 div 1
if (units is 0) then
set end of o's collector to "zero"
else
set end of o's collector to item (units div 1) of o's unitsAndTeens
end if
set n to n * 10.0
if (n mod 10 is 0.0) then exit repeat
end repeat
end if
-- Coerce the assembled words to a single text.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set English to o's collector as text
set AppleScript's text item delimiters to astid
return English
end numberToEnglish
 
numberToEnglish from -3.60287970189634E+12
--> "minus three trillion six hundred and two billion eight hundred and seventy-nine million seven hundred and one thousand eight hundred and ninety-six point three four"
numberToEnglish from -3.60287970189634E+12 without ands
--> "minus three trillion six hundred two billion eight hundred seventy-nine million seven hundred one thousand eight hundred ninety-six point three four"
numberToEnglish from -3.60287970189634E+12 with longScale
--> "minus three billion six hundred and two thousand eight hundred and seventy-nine million seven hundred and one thousand eight hundred and ninety-six point three four"
numberToEnglish from -3.60287970189634E+12 with milliards
--> "minus three billion six hundred and two milliard eight hundred and seventy-nine million seven hundred and one thousand eight hundred and ninety-six point three four"</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Handles zero and negative integers. Rounding errors occur with big numbers.
<langsyntaxhighlight ApplesoftBASIClang="applesoftbasic">10 INPUT "GIMME A NUMBER! "; N
20 GOSUB 100"NUMBER NAME
30 PRINT R$
Line 564 ⟶ 747:
288 DATA "EIGHT", "EIGHTEEN", "EIGHTY", "OCTILLION"
289 DATA "NINE", "NINETEEN", "NINETY", "NONILLION"
290 DATA "", "", "", "DECILLION"</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Loop { ; TEST LOOP
n =
Random Digits, 1, 36 ; random number with up to 36 digits
Line 606 ⟶ 789:
PrettyNumber(n) { ; inserts thousands separators into a number string
Return RegExReplace( RegExReplace(n,"^0+(\d)","$1"), "\G\d+?(?=(\d{3})+(?:\D|$))", "$0,")
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NUMBER_NAMES.AWK
BEGIN {
Line 661 ⟶ 844:
split("ten twenty thirty forty fifty sixty seventy eighty ninety",tens," ")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 679 ⟶ 862:
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION int2Text$ (number AS LONG)
 
<lang qbasic>DECLARE FUNCTION int2Text$ (number AS LONG)
 
'small
Line 752 ⟶ 934:
 
int2Text$ = RTRIM$(outP)
END FUNCTION</langsyntaxhighlight>
 
{{out|Sample outputs}} (including the answer to the ultimate question of life, the universe, and everything):
<pre> Gimme a number! 1
Gimme a number! 1
one
Gimme a number! 0
Line 769 ⟶ 949:
one billion one
Gimme a number! &h7fffffff
two billion one hundred forty-seven million four hundred eighty-three thousand six hundred forty-seven</pre>
 
</pre>
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">global lows
lows = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
global tens
tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
global lev
lev = {"", "thousand", "million", "billion"}
 
function numname_int(n)
if n = 0 then return "zero"
if n < 0 then return "negative " + numname_int(-n)
t = -1
redim triples(3) #con < 3 da error ¿¿??
ret = ""
while n > 0
t += 1
triples[t] = n mod 1000
n = int(n / 1000)
end while
for i = t to 0 step -1
tripname = ""
if triples[i] = 0 then continue for
lasttwo = triples[i] mod 100
hundreds = triples[i] \ 100
if lasttwo < 20 then
tripname = lows[lasttwo] + tripname + " "
else
tripname = tens[lasttwo\10] + "-" + lows[lasttwo mod 10] + " " + tripname
end if
if hundreds > 0 then
if lasttwo > 0 then tripname = " and " + tripname
tripname = lows[hundreds] + " hundred" + tripname
end if
if i = 0 and t > 0 and hundreds = 0 then tripname = " and " + tripname
tripname += lev[i] + " "
ret = ltrim(ret) + ltrim(tripname)
next i
return trim(ret)
end function
 
function numname(n)
ret = ""
if n = int(n) then return numname_int(int(n))
prefix = numname_int(int(abs(n))) + " point "
decdig = string(abs(n)-int(abs(n)))
if n < 0 then prefix = "negative " + prefix
ret = prefix
for i = 3 to length(decdig)
ret += numname(int(mid(decdig,i,1))) + " "
next i
return trim(ret)
end function
 
print numname(0)
print numname(1.0)
print numname(-1.7)
print numname(910000)
print numname(987654)
print numname(100000017)</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Batch File}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="dos">::Number Names Task from Rosetta Code Wiki
::Batch File Implementation
 
Line 849 ⟶ 1,092:
 
set TotalOut=%TotalOut:~0,-1%
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre>>NUMBER.BAT
Line 865 ⟶ 1,108:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM test%(20)
test%() = 0, 1, 2, 19, 20, 21, 99, 100, 101, 300, 310, 1001, -1327, 1501, \
\ 10203, 12609, 101104, 102003, 467889, 1005006, -123000789
Line 904 ⟶ 1,147:
ENDIF
NEXT i%
= a$</langsyntaxhighlight>
{{out}}
<pre>
Line 929 ⟶ 1,172:
minus one hundred and twenty-three million, seven hundred and eighty-nine
</pre>
 
=={{header|BCPL}}==
This will work up to and including 999999, or up to the maximum of the system word size, whichever is smaller.
 
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( MAXLEN = 256/BYTESPERWORD $)
 
let append(v1, v2) be
$( for i=1 to v2%0 do
v1%(v1%0+i) := v2%i
v1%0 := v1%0 + v2%0
$)
 
let spell(n, v) = valof
$( let small(n) =
n=0 -> "", n=1 -> "one", n=2 -> "two",
n=3 -> "three", n=4 -> "four", n=5 -> "five",
n=6 -> "six", n=7 -> "seven", n=8 -> "eight",
n=9 -> "nine", n=10 -> "ten", n=11 -> "eleven",
n=12 -> "twelve", n=13 -> "thirteen", n=14 -> "fourteen",
n=15 -> "fifteen", n=16 -> "sixteen", n=17 -> "seventeen",
n=18 -> "eighteen", n=19 -> "nineteen", valof finish
let teens(n) =
n=2 -> "twenty", n=3 -> "thirty", n=4 -> "forty",
n=5 -> "fifty", n=6 -> "sixty", n=7 -> "seventy",
n=8 -> "eighty", n=9 -> "ninety", valof finish
let less100(n, v) be
$( if n >= 20
$( append(v, teens(n/10))
n := n rem 10
unless n=0 append(v, "-")
$)
append(v, small(n))
$)
let inner(n, v) be
$( let step(n, val, name, v) = valof
$( if n>=val
$( inner(n/val, v)
append(v, name)
unless n=val do append(v, " ")
$)
resultis n rem val
$)
test n<0
$( append(v, "minus ")
inner(-n, v)
$)
or
$( n := step(n, 1000, " thousand", v)
n := step(n, 100, " hundred", v)
less100(n, v)
$)
$)
v%0 := 0
test n=0 do append(v, "zero") or inner(n, v)
resultis v
$)
 
let reads(v) be
$( v%0 := 0
$( let c = rdch()
if c='*N' | c=endstreamch break
v%0 := v%0 + 1
v%(v%0) := c
$) repeat
$)
 
let atoi(v) = valof
$( let r = 0 and neg = false and i = 1
if v%1 = '-' then
$( i := 2
neg := true
$)
while '0' <= v%i <= '9' & i <= v%0
$( r := r*10 + v%i-'0'
i := i+1
$)
resultis neg -> -r, r
$)
 
let start() be
$( let instr = vec MAXLEN and numstr = vec MAXLEN
$( writes("Number? ")
reads(instr)
if instr%0=0 finish
writef("%N: %S*N", atoi(instr), spell(atoi(instr), numstr))
$) repeat
$)</syntaxhighlight>
{{out}}
<pre>Number? 0
0: zero
Number? 19
19: nineteen
Number? 65
65: sixty-five
Number? 123
123: one hundred twenty-three
Number? -456
-456: minus four hundred fifty-six
Number? 30000
30000: thirty thousand</pre>
 
=={{header|BlitzMax}}==
{{trans|C++}}
 
<langsyntaxhighlight BlitzMaxlang="blitzmax">SuperStrict
 
Framework BRL.StandardIO
Line 1,013 ⟶ 1,361:
Local numberSpell:TSpell = New TSpell
Print number + " " + numberSpell.spell(number)
End Function</langsyntaxhighlight>
<pre>
99 ninety-nine
Line 1,026 ⟶ 1,374:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 1,162 ⟶ 1,510:
say_number("123456789012345678901234567890123456789012345678900000001");
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>minus forty-two
Line 1,170 ⟶ 1,518:
one trillion, one billion, one million, one thousand, and one
one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine trillion trillion trillion trillion, twelve billion three hundred forty-five million six hundred seventy-eight thousand nine hundredand one trillion trillion trillion, two hundred thirty-four billion five hundred sixty-seven million eight hundred ninety thousand one hundred twenty-three trillion trillion, four hundred fifty-six billion seven hundred eighty-nine million twelve thousand three hundred forty-five trillion, six hundred seventy-eight billion, nine hundred million, and one</pre>
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <iostream>
using std::string;
 
const char* smallNumbers[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
};
string spellHundreds(unsigned n) {
string res;
if (n > 99) {
res = smallNumbers[n/100];
res += " hundred";
n %= 100;
if (n) res += " and ";
}
if (n >= 20) {
static const char* Decades[] = {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
res += Decades[n/10];
n %= 10;
if (n) res += "-";
}
if (n < 20 && n > 0)
res += smallNumbers[n];
return res;
}
 
 
const char* thousandPowers[] = {
" billion", " million", " thousand", "" };
 
typedef unsigned long Spellable;
 
string spell(Spellable n) {
if (n < 20) return smallNumbers[n];
string res;
const char** pScaleName = thousandPowers;
Spellable scaleFactor = 1000000000; // 1 billion
while (scaleFactor > 0) {
if (n >= scaleFactor) {
Spellable h = n / scaleFactor;
res += spellHundreds(h) + *pScaleName;
n %= scaleFactor;
if (n) res += ", ";
}
scaleFactor /= 1000;
++pScaleName;
}
return res;
}
 
int main() {
#define SPELL_IT(x) std::cout << #x " " << spell(x) << std::endl;
SPELL_IT( 99);
SPELL_IT( 300);
SPELL_IT( 310);
SPELL_IT( 1501);
SPELL_IT( 12609);
SPELL_IT( 512609);
SPELL_IT(43112609);
SPELL_IT(1234567890);
return 0;
}</lang>
{{out}}
<pre>
99 ninety-nine
300 three hundred
310 three hundred and ten
1501 one thousand, five hundred and one
12609 twelve thousand, six hundred and nine
512609 five hundred and twelve thousand, six hundred and nine
43112609 forty-three million, one hundred and twelve thousand, six hundred and nine
1234567890 one billion, two hundred and thirty-four million, five hundred and sixty-seven thousand, eight hundred and ninety
</pre>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|2.0+, works for numbers between 0 and 999,999,999}}
 
<langsyntaxhighlight lang="csharp">using System;
 
class NumberNamer {
Line 1,334 ⟶ 1,600:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,341 ⟶ 1,607:
thirty one thousand, three hundred and thirty seven
nine hundred and eighty seven million, six hundred and fifty four thousand, three hundred and twenty one
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <string>
#include <iostream>
using std::string;
 
const char* smallNumbers[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
};
string spellHundreds(unsigned n) {
string res;
if (n > 99) {
res = smallNumbers[n/100];
res += " hundred";
n %= 100;
if (n) res += " and ";
}
if (n >= 20) {
static const char* Decades[] = {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
res += Decades[n/10];
n %= 10;
if (n) res += "-";
}
if (n < 20 && n > 0)
res += smallNumbers[n];
return res;
}
 
 
const char* thousandPowers[] = {
" billion", " million", " thousand", "" };
 
typedef unsigned long Spellable;
 
string spell(Spellable n) {
if (n < 20) return smallNumbers[n];
string res;
const char** pScaleName = thousandPowers;
Spellable scaleFactor = 1000000000; // 1 billion
while (scaleFactor > 0) {
if (n >= scaleFactor) {
Spellable h = n / scaleFactor;
res += spellHundreds(h) + *pScaleName;
n %= scaleFactor;
if (n) res += ", ";
}
scaleFactor /= 1000;
++pScaleName;
}
return res;
}
 
int main() {
#define SPELL_IT(x) std::cout << #x " " << spell(x) << std::endl;
SPELL_IT( 99);
SPELL_IT( 300);
SPELL_IT( 310);
SPELL_IT( 1501);
SPELL_IT( 12609);
SPELL_IT( 512609);
SPELL_IT(43112609);
SPELL_IT(1234567890);
return 0;
}</syntaxhighlight>
{{out}}
<pre>
99 ninety-nine
300 three hundred
310 three hundred and ten
1501 one thousand, five hundred and one
12609 twelve thousand, six hundred and nine
512609 five hundred and twelve thousand, six hundred and nine
43112609 forty-three million, one hundred and twelve thousand, six hundred and nine
1234567890 one billion, two hundred and thirty-four million, five hundred and sixty-seven thousand, eight hundred and ninety
</pre>
 
Line 1,346 ⟶ 1,694:
{{trans|Common Lisp}}
 
<langsyntaxhighlight lang="clojure">(clojure.pprint/cl-format nil "~R" 1234)
=> "one thousand, two hundred thirty-four"</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="coffeescript">
spell_integer = (n) ->
tens = [null, null, "twenty", "thirty", "forty",
Line 1,410 ⟶ 1,758:
console.log spell_integer 2010
console.log spell_integer 4000123007913
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,438 ⟶ 1,786:
 
 
<langsyntaxhighlight lang="commodorebasicv2">10 print chr$(147);chr$(14)
20 dim s$(11),ts$(11),t$(11),o$(11):co=39
30 for i=0 to 11:read s$(i),ts$(i),t$(i),o$(i):next
Line 1,485 ⟶ 1,833:
1009 data nine,nineteen,ninety,octillion
1010 data "","","",nonillion
1011 data "","","",decillion</langsyntaxhighlight>
 
{{out}}
Line 1,535 ⟶ 1,883:
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">(format nil "~R" 1234)
=> "one thousand two hundred thirty-four"</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Python}}
use -version=number_names_main to see the output.
<langsyntaxhighlight lang="d">import std.stdio, std.array, std.algorithm, std.bigint, std.range;
 
immutable tens = ["", "", "twenty", "thirty", "forty",
Line 1,618 ⟶ 1,966:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre> +0 -> zero
Line 1,643 ⟶ 1,991:
2 -> two
0 -> zero</pre>
=={{header|Delphi}}==
{{Trans|Pascal}}
Adaptation of [[#Pascal]] Program to run in delphi.
<syntaxhighlight lang="delphi">
program Number_names;
 
{$APPTYPE CONSOLE}
 
const
smallies: array[1..19] of string = ('one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen');
tens: array[2..9] of string = ('twenty', 'thirty', 'forty', 'fifty', 'sixty',
'seventy', 'eighty', 'ninety');
 
function domaxies(number: int64): string;
const
maxies: array[0..5] of string = (' thousand', ' million', ' billion',
' trillion', ' quadrillion', ' quintillion');
begin
domaxies := '';
if number >= 0 then
domaxies := maxies[number];
end;
 
function doHundreds(number: int64): string;
begin
Result := '';
if number > 99 then
begin
Result := smallies[number div 100];
Result := Result + ' hundred';
number := number mod 100;
if number > 0 then
Result := Result + ' and ';
end;
if number >= 20 then
begin
Result := Result + tens[number div 10];
number := number mod 10;
if number > 0 then
Result := Result + '-';
end;
if (0 < number) and (number < 20) then
Result := Result + smallies[number];
end;
 
function spell(number: int64): string;
var
scaleFactor: int64;
maxieStart, h: int64;
begin
Result := '';
if number < 0 then
begin
number := -number;
Result := 'negative ';
end;
scaleFactor := 1000000000000000000;
maxieStart := 5;
if number < 20 then
exit(Result+smallies[number]);
while scaleFactor > 0 do
begin
if number > scaleFactor then
begin
h := number div scaleFactor;
Result := Result + doHundreds(h) + domaxies(maxieStart);
number := number mod scaleFactor;
if number > 0 then
Result := Result + ', ';
end;
scaleFactor := scaleFactor div 1000;
dec(maxieStart);
end;
end;
 
begin
writeln(99, ': ', spell(99));
writeln(234, ': ', spell(234));
writeln(7342, ': ', spell(7342));
writeln(32784, ': ', spell(32784));
writeln(234345, ': ', spell(234345));
writeln(2343451, ': ', spell(2343451));
writeln(23434534, ': ', spell(23434534));
writeln(234345456, ': ', spell(234345456));
writeln(2343454569, ': ', spell(2343454569));
writeln(2343454564356, ': ', spell(2343454564356));
writeln(2345286538456328, ': ', spell(2345286538456328));
Readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
small$[] = [ "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen" ]
tens$[] = [ "" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety" ]
illions$[] = [ "" " thousand" " million" " billion" " trillion" " quadrillion" " quintillion" ]
func$ say n .
if n < 0
t$ = "negative "
n = -n
.
if n < 20
t$ &= small$[n + 1]
elif n < 100
t$ &= tens$[n div 10 + 1]
s = n mod 10
if s > 0
t$ &= "-" & small$[s + 1]
.
elif n < 1000
t$ &= small$[n div 100 + 1] & " hundred"
s = n mod 100
if s > 0
t$ &= " " & say s
.
else
i = 1
while n > 0
p = n mod 1000
n = n div 1000
if p > 0
ix$ = say p & illions$[i]
if sx$ <> ""
ix$ &= " " & sx$
.
sx$ = ix$
.
i += 1
.
t$ &= sx$
.
return t$
.
for n in [ 12 1048576 9e18 -2 0 ]
print say n
.
</syntaxhighlight>
{{out}}
<pre>
twelve
one million forty-eight thousand five hundred seventy-six
nine quintillion
negative two
zero
</pre>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module NumberNames {
void run() {
@Inject Console console;
 
Int[] tests = [0, 1, -1, 11, -17, 42, 99, 100, 101, -111, 1000, 1234, 10000, 100000,
123456789000, 0x123456789ABCDEF];
for (Int test : tests) {
console.print($"{test} = {toEnglish(test)}");
}
}
 
static String[] digits = ["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"];
static String[] teens = ["ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
static String[] tens = ["zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"];
static String[] ten3rd = ["?", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion"];
 
static String toEnglish(Int n) {
StringBuffer buf = new StringBuffer();
if (n < 0) {
"negative ".appendTo(buf);
n = -n;
}
 
format3digits(n, buf);
return buf.toString();
}
 
static void format3digits(Int n, StringBuffer buf, Int nested=0) {
(Int left, Int right) = n /% 1000;
if (left != 0) {
format3digits(left, buf, nested+1);
}
 
if (right != 0 || (left == 0 && nested==0)) {
if (right >= 100) {
(left, right) = (right /% 100);
digits[left].appendTo(buf);
" hundred ".appendTo(buf);
if (right != 0) {
format2digits(right, buf);
}
} else {
format2digits(right, buf);
}
 
if (nested > 0) {
ten3rd[nested].appendTo(buf).add(' ');
}
}
}
 
static void format2digits(Int n, StringBuffer buf) {
switch (n) {
case 0..9:
digits[n].appendTo(buf).add(' ');
break;
 
case 10..19:
teens[n-10].appendTo(buf).add(' ');
break;
 
default:
(Int left, Int right) = n /% 10;
tens[left].appendTo(buf);
if (right == 0) {
buf.add(' ');
} else {
buf.add('-');
digits[right].appendTo(buf).add(' ');
}
break;
}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
0 = zero
1 = one
-1 = negative one
11 = eleven
-17 = negative seventeen
42 = forty-two
99 = ninety-nine
100 = one hundred
101 = one hundred one
-111 = negative one hundred eleven
1000 = one thousand
1234 = one thousand two hundred thirty-four
10000 = ten thousand
100000 = one hundred thousand
123456789000 = one hundred twenty-three billion four hundred fifty-six million seven hundred eighty-nine thousand
81985529216486895 = eighty-one quadrillion nine hundred eighty-five trillion five hundred twenty-nine billion two hundred sixteen million four hundred eighty-six thousand eight hundred ninety-five
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
@small ~w(zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen
Line 1,693 ⟶ 2,290:
e in ArgumentError -> IO.puts Exception.message(e)
end
end)</langsyntaxhighlight>
 
{{out}}
Line 1,719 ⟶ 2,316:
=={{header|Erlang}}==
 
<langsyntaxhighlight lang="erlang">
-module(nr2eng).
-import(lists, [foreach/2, seq/2, append/2]).
Line 1,797 ⟶ 2,394:
end,
append(seq(1, 2000), [123123, 43234234])).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,808 ⟶ 2,405:
=={{header|Euphoria}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="euphoria">function abs(atom i)
if i < 0 then
return -i
Line 1,879 ⟶ 2,476:
puts(1,int2text(1234567890) & "\n")
puts(1,int2text(-987654321) & "\n")
puts(1,int2text(0) & "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,889 ⟶ 2,486:
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="fsharp">let divMod n d = n / d, n % d
 
let join = String.concat ", "
Line 1,946 ⟶ 2,543:
join (List.map big fsegn)
;;</langsyntaxhighlight>
 
=={{header|Factor}}==
Factor "cheats" by having a standard library module for this task:
 
<langsyntaxhighlight lang="factor">IN: scratchpad USE: math.text.english
IN: scratchpad 43112609 number>text print
forty-three million, one hundred and twelve thousand, six hundred and nine
</syntaxhighlight>
</lang>
 
=={{header|FōrmulæFreeBASIC}}==
Can handle floating point numbers, and negatives, with absolute value less than one billion.
<syntaxhighlight lang="freebasic">dim shared as string*9 lows(0 to 19) = {"", "one", "two", "three", "four",_
"five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",_
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",_
"eighteen", "nineteen"}
dim shared as string*7 tens(0 to 9) = {"", "", "twenty", "thirty", "forty",_
"fifty", "sixty", "seventy", "eighty", "ninety"}
dim shared as string*8 lev(0 to 3) = {"", "thousand", "million", "billion"}
 
function numname_int( byval n as integer ) as string
In [https://wiki.formulae.org/Data_types_tutorial#Number_names this] page you can see the solution of this task.
if n = 0 then return "zero"
if n < 0 then return "negative " + numname_int( -n )
dim as integer t = -1, lasttwo, hundreds
redim as integer triples(0 to 0)
dim as string ret, tripname
while n > 0
t += 1
triples(t) = n mod 1000
n \= 1000
wend
for i as integer = t to 0 step -1
tripname = ""
if triples(i) = 0 then continue for
lasttwo = triples(i) mod 100
hundreds = triples(i)\100
if lasttwo < 20 then
tripname = lows(lasttwo) + tripname + " "
else
tripname = tens(lasttwo\10) + "-" + lows(lasttwo mod 10) + " " + tripname
end if
if hundreds > 0 then
if lasttwo > 0 then tripname = " and " + tripname
tripname = lows(hundreds)+" hundred" + tripname
end if
if i=0 and t>0 and hundreds = 0 then tripname = " and " + tripname
tripname += lev(i)+" "
ret = ltrim(ret) + ltrim(tripname)
next i
return ltrim(rtrim(ret))
end function
 
function numname( n as double ) as string
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
if n=int(n) then return numname_int(int(n))
dim as string prefix = numname_int( int(abs(n)) )+" point "
dim as string decdig = str(abs(n)-int(abs(n))), ret
if n < 0 then prefix = "negative "+prefix
ret = prefix
for i as uinteger = 3 to len(decdig)
ret = ret + numname(val(mid(decdig,i,1)))+" "
next i
return ltrim(rtrim(ret))
end function
 
print numname(0)
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
print numname(1.0)
print numname(-1.7)
print numname(910000)
print numname(987654)
print numname(100000017)
</syntaxhighlight>
{{out}}
<pre>
zero
one
negative one point seven
nine hundred and ten thousand
nine hundred and eighty-seven thousand six hundred and fifty-four
one hundred million and seventeen
</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program spell
 
implicit none
Line 2,032 ⟶ 2,692:
end do
 
end program spell</langsyntaxhighlight>
Sample input:
<pre>-1
Line 2,045 ⟶ 2,705:
forty-two
two billion one hundred forty-seven million four hundred eighty-three thousand six hundred forty-seven</pre>
</pre>
 
 
=={{header|FutureBasic}}==
FB has native convenience functions making conversion of numbers to strings for many locales and languages an easy task.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn IntegerToOrdinalString( number as CFNumberRef ) as CFStringRef
NumberFormatterRef numberFormatter = fn NumberFormatterInit
NumberFormatterSetNumberStyle( numberFormatter, NSNumberFormatterSpellOutStyle )
NumberFormatterSetLocale( numberFormatter, fn LocaleWithIdentifier( @"en_US" ) )
CFStringRef numberStr = fn NumberFormatterStringFromNumber( numberFormatter, number )
end fn = numberStr
 
CFArrayRef numArr
CFStringRef numStr
CFNumberRef number
numStr = @" 1 2 3 4 5 11 65 100 101 272 23456 8007006005004003 00123.0"
numArr = fn StringComponentsSeparatedByString( numStr, @" " )
 
for numStr in numArr
number = fn NumberWithInteger( fn StringIntegerValue( numStr ) )
NSLog( @"%16ld : %@", fn StringIntegerValue( numStr ), fn IntegerToOrdinalString( number ) )
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
0 : zero
1 : one
2 : two
3 : three
4 : four
5 : five
11 : eleven
65 : sixty-five
100 : one hundred
101 : one hundred one
272 : two hundred seventy-two
23456 : twenty-three thousand four hundred fifty-six
8007006005004003 : eight quadrillion seven trillion six billion five million four thousand three
123 : one hundred twenty-three
</pre>
 
 
 
=={{header|Go}}==
Supports integers from <code>math.MinInt64 + 1</code> to <code>math.MaxInt64</code>.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,105 ⟶ 2,812:
}
return t
}</langsyntaxhighlight>
Output:
<pre>
Line 2,116 ⟶ 2,823:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def divMod(BigInteger number, BigInteger divisor) {
def qr = number.divideAndRemainder(divisor)
[div:qr[0], remainder:qr[1]]
Line 2,190 ⟶ 2,897:
verifyToText 'one million and forty five', 1000045
verifyToText 'one million and fifteen', 1000015
verifyToText 'one billion, forty five thousand and one', 1000045001</langsyntaxhighlight>
Output:
<pre>twenty nine
Line 2,226 ⟶ 2,933:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, unfoldr)
 
spellInteger :: Integer -> String
Line 2,261 ⟶ 2,968:
big (e, n) = spellInteger n ++ ' ' : (l !! e) ++ "illion"
where l = [undefined, undefined, "m", "b", "tr", "quadr",
"quint", "sext", "sept", "oct", "non", "dec"]</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">SUBROUTINE NumberToWords(number)
CHARACTER outP*255, small*130, tens*80, big*80
REAL :: decimal_places = 7
Line 2,333 ⟶ 3,040:
TENS=ten twenty thirty forty fifty sixty seventy eighty ninety
 
BIG=thousand million billion trillion quadrillion</langsyntaxhighlight>
<langsyntaxhighlight HicEstlang="hicest">0 = zero
1234 = one thousand and two hundred thirty-four
12.34 = twelve point three four
Line 2,340 ⟶ 3,047:
32768 = thirty-two thousand and seven hundred sixty-eight
1E-3 = point zero zero one
-2.7182818 = minus two point seven one eight two eight one eight</langsyntaxhighlight>
 
== {{header|Icon}} and {{header|Unicon}} ==
<langsyntaxhighlight Iconlang="icon">link numbers # commas, spell
 
procedure main(arglist)
every x := !arglist do
write(commas(x), " -> ",spell(x))
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers:spell] was used as a based for this procedure.
 
<langsyntaxhighlight Iconlang="icon">procedure spell(n) #: spell out integer (short scale)
local m, i
static scale
Line 2,389 ⟶ 3,096:
}
else fail # really big
end</langsyntaxhighlight>
Sample output:
<pre>#spell.exe 5 11 15 67 10132767 65535 -1234567890123456
Line 2,403 ⟶ 3,110:
=={{header|Inform 7}}==
{{works with|Z-machine}}
<syntaxhighlight lang ="inform7">say 32767 in words;</langsyntaxhighlight>
 
{{works with|Glulx virtual machine}}
<syntaxhighlight lang ="inform7">say 2147483647 in words;</langsyntaxhighlight>
 
== {{header|J}} ==
'''Solutions:'''
<langsyntaxhighlight lang="j">u=. ;:'one two three four five six seven eight nine'
v=. ;:'ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'
t=. ;:'twenty thirty forty fifty sixty seventy eighty ninety'
Line 2,433 ⟶ 3,140:
 
uk=: ' and '&en NB. British
us=: ' ' &en NB. American</langsyntaxhighlight>
 
'''Example:'''
Line 2,442 ⟶ 3,149:
us 1234567890123456789012345678901234567890123456789012345678901234567890x
one duovigintillion, two hundred thirty-four unvigintillion, five hundred sixty-seven vigintillion, eight hundred ninety novemdecillion, one hundred twenty-three octodecillion, four hundred fifty-six septendecillion, seven hundred eighty-nine sexdecillion, twelve quindecillion, three hundred forty-five quattuordecillion, six hundred seventy-eight tredecillion, nine hundred one duodecillion, two hundred thirty-four undecillion, five hundred sixty-seven decillion, eight hundred ninety nonillion, one hundred twenty-three octillion, four hundred fifty-six septillion, seven hundred eighty-nine sextillion, twelve quintillion, three hundred forty-five quadrillion, six hundred seventy-eight trillion, nine hundred one billion, two hundred thirty-four million, five hundred sixty-seven thousand, eight hundred ninety</pre>
 
See also: [https://code.jsoftware.com/wiki/Essays/Number_in_Words Number in Words essay on J wiki]
 
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">public enum IntToWords {
;
 
Line 2,522 ⟶ 3,231:
return sb.toString().trim();
}
}</langsyntaxhighlight>
Output:
<pre>zero
Line 2,541 ⟶ 3,250:
===Recursive===
 
<langsyntaxhighlight lang="java">public class NumberToWordsConverter { // works upto 9999999
 
final private static String[] units = {"Zero","One","Two","Three","Four",
Line 2,558 ⟶ 3,267:
return convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
{{trans|Groovy}}
<langsyntaxhighlight JavaScriptlang="javascript">const divMod = y => x => [Math.floor(y/x), y % x];
 
const sayNumber = value => {
Line 2,605 ⟶ 3,314:
}
return name;
};</langsyntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">
<lang Joy>
DEFINE units ==
[ "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten"
"eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen"
"eighteen" "nineteen" ];
 
tens ==
[ "ten" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety" ];
 
convert6 ==
Line 2,652 ⟶ 3,361:
[convert2]
ifte.
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq"># Adapted from the go version.
# Tested with jq 1.4
#
Line 2,715 ⟶ 3,424:
end ;
 
say</langsyntaxhighlight>
Transcript (input followed by output):
<langsyntaxhighlight lang="jq">0
"zero"
-0
Line 2,733 ⟶ 3,442:
12345678912345678
"twelve quadrillion, three hundred and forty five trillion, six hundred and seventy eight billion, nine hundred and twelve million, three hundred and forty five thousand, six hundred and seventy eight"
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Line 2,739 ⟶ 3,448:
 
'''Number Names Functions'''
<langsyntaxhighlight Julialang="julia">const stext = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
const teentext = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"]
Line 2,803 ⟶ 3,512:
n < toobig || return "too big to say"
digits2text!(digits(n, base=10), use_short_scale)
end</langsyntaxhighlight>
 
'''Main'''
<langsyntaxhighlight Julialang="julia">using Printf
 
println("Some easy ones to start with\n")
Line 2,848 ⟶ 3,557:
println(" ", i, " is")
println(num2text(i, false))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,943 ⟶ 3,652:
 
There is an option to use the UK (rather than the US) method of spelling out numbers whereby 'and' is placed at strategic positions.
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val oneNames = listOf(
Line 3,015 ⟶ 3,724:
println("Using UK representation:")
for (i in exampleNumbers) println("${"%20d".format(i)} = ${numToText(i, true)}")
}</langsyntaxhighlight>
 
{{out}}
Line 3,054 ⟶ 3,763:
1234000000745003 = one quadrillion, two hundred and thirty-four trillion, seven hundred and forty-five thousand and three
-9223372036854775808 = minus nine quintillion, two hundred and twenty-three quadrillion, three hundred and seventy-two trillion, thirty-six billion, eight hundred and fifty-four million, seven hundred and seventy-five thousand, eight hundred and eight
</pre>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
global outnum$
dim ones$(20),tens$(9),gr$(5),group(5)
for a = 0 to 19
read a$:ones$(a)=a$
next a
for a = 1 to 8
read a$:tens$(a)=a$
next a
 
[start]
redim gr$(5)
redim group(5)
 
input "Enter a number (nonzero positive integers only less than 1 quadrillion, no commas): ";num$
 
num$=trim$(num$)
numVal=int(val(num$))
if numVal=0 or numVal>999999999999999 then print "Ended" : end
 
numParse = numVal
numLen = len(str$(numParse))
 
if numLen<=15 then groupCount=5
if numLen<=12 then groupCount=4
if numLen<=9 then groupCount=3
if numLen<=6 then groupCount=2
if numLen<=3 then groupCount=1
 
if numLen>12 and numLen<=15 then
group(5)=int(numParse/1000000000000)
call convert group(5)," trillion"
gr$(5)=outnum$
numParse=nP(numParse,12)
numLen=len(str$(numParse))
end if
 
if numLen>9 and numLen<=12 then
group(4)=int(numParse/1000000000)
call convert group(4)," billion"
gr$(4)=outnum$
numParse=nP(numParse,9)
numLen=len(str$(numParse))
end if
 
if numLen>6 and numLen<=9 then
group(3)=int(numParse/1000000)
call convert group(3)," million"
gr$(3)=outnum$
numParse=nP(numParse,6)
numLen=len(str$(numParse))
end if
 
if numLen>3 and numLen<=6 then
group(2)=int(numParse/1000)
call convert group(2)," thousand"
gr$(2)=outnum$
numParse=nP(numParse,3)
numLen=len(str$(numParse))
end if
 
if numLen<=3 then
group(1)=numParse
call convert group(1),""
gr$(1)=outnum$
numLen=len(str$(numParse))
end if
 
print
for a=groupCount to 1 step -1
print gr$(a);" ";
next a
print
print
 
goto [start]
 
sub convert num, dem$
outnum$=""
for a=len(str$(num)) to 1 step -1
b$=mid$(str$(num),a,1)
c=val(b$)
d=len(str$(num))-a+1
select case
case d=1
outnum$=ones$(c)
case d=2
if c<2 then
outnum$=ones$(val(right$(str$(num),2)))
else
if c>=2 and val(right$(str$(num),1))<>0 then
outnum$=tens$(c-1)+"-"+outnum$
else
outnum$=tens$(c-1)
end if
end if
case d=3
if c<>0 then outnum$=ones$(c)+" hundred "+outnum$
end select
next a
outnum$=outnum$+dem$
end sub
 
function nP(num,L)
nP=val(right$(str$(num),L))
end function
 
data "","one","two","three","four","five","six","seven","eight","nine"
data "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
data "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
</syntaxhighlight>
{{out}}
<pre>
Entering 789456019600009 yields
seven hundred eighty-nine trillion four hundred fifty-six billion nineteen million six hundred thousand nine
</pre>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">make "numbers {one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
 
Line 3,084 ⟶ 3,911:
end
 
print to.english 1234567 ; one million two hundred thirty four thousand five hundred sixty seven</langsyntaxhighlight>
 
=={{header|Lua}}==
===Original===
 
<langsyntaxhighlight lang="lua">words = {"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "}
levels = {"thousand ", "million ", "billion ", "trillion ", "quadrillion ", "quintillion ", "sextillion ", "septillion ", "octillion ", [0] = ""}
iwords = {"ten ", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "}
Line 3,124 ⟶ 3,951:
end
 
if #vword == 0 then print "zero" else print(vword) end</langsyntaxhighlight>
 
===Alternate===
As used in [[Four is magic#Lua|Four is magic]]
<syntaxhighlight lang="lua">-- Number names, in Lua, 6/17/2020 db
local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }
local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }
local tenslist = { [0]="", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }
local lionlist = { [0]="", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion" }
local abs, floor = math.abs, math.floor
 
local function numname(num)
if (num == 0) then return "zero" end
local absnum, lion, result = abs(num), 0, ""
local function dashed(s) return s=="" and s or "-"..s end
local function spaced(s) return s=="" and s or " "..s end
while (absnum > 0) do
local word, ones, tens, huns = "", absnum%10, floor(absnum/10)%10, floor(absnum/100)%10
if (tens==0) then word = oneslist[ones]
elseif (tens==1) then word = teenlist[ones]
else word = tenslist[tens] .. dashed(oneslist[ones]) end
if (huns > 0) then word = oneslist[huns] .. " hundred" .. spaced(word) end
if (word ~= "") then result = word .. spaced(lionlist[lion]) .. spaced(result) end
absnum = floor(absnum / 1000)
lion = lion + 1
end
if (num < 0) then result = "negative " .. result end
return result
end
 
local numbers = {
-1, 0, 1,
10, 15, 20, 21,
123, 300, 301, 320, 321,
1e3, 1e3+1, 1010, 1011, 1100, 1101, 1110, 1111, 1234, 4321, 12345, 54321, 123456, 654321,
1e6, 1e6+1, 1001000, 1234567, 12345678, 123456789, 987654321,
1e9, 1e9+1, 1000001000, 1001000000, 1001001000, 1234567890, 12345678901, 123456789012, 210987654321,
1e12, 1e12+1, 1000000001000, 1000001000000, 1000001001000, 1001001001001, 1234567890123, 12345678901234, 123456789012345, 543210987654321,
1e15, 1e15+1, 1010101010101010, 1234567890123456, 6543210987654321,
-- limit of precision, pushing it..
1e18, 1e21,
}
for _, num in ipairs(numbers) do
print( string.format("%.f: '%s'", num, numname(num)) )
end</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">-1: 'negative one'
0: 'zero'
1: 'one'
10: 'ten'
15: 'fifteen'
20: 'twenty'
21: 'twenty-one'
123: 'one hundred twenty-three'
300: 'three hundred'
301: 'three hundred one'
320: 'three hundred twenty'
321: 'three hundred twenty-one'
1000: 'one thousand'
1001: 'one thousand one'
1010: 'one thousand ten'
1011: 'one thousand eleven'
1100: 'one thousand one hundred'
1101: 'one thousand one hundred one'
1110: 'one thousand one hundred ten'
1111: 'one thousand one hundred eleven'
1234: 'one thousand two hundred thirty-four'
4321: 'four thousand three hundred twenty-one'
12345: 'twelve thousand three hundred forty-five'
54321: 'fifty-four thousand three hundred twenty-one'
123456: 'one hundred twenty-three thousand four hundred fifty-six'
654321: 'six hundred fifty-four thousand three hundred twenty-one'
1000000: 'one million'
1000001: 'one million one'
1001000: 'one million one thousand'
1234567: 'one million two hundred thirty-four thousand five hundred sixty-seven'
12345678: 'twelve million three hundred forty-five thousand six hundred seventy-eight'
123456789: 'one hundred twenty-three million four hundred fifty-six thousand seven hundred eighty-nine'
987654321: 'nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000: 'one billion'
1000000001: 'one billion one'
1000001000: 'one billion one thousand'
1001000000: 'one billion one million'
1001001000: 'one billion one million one thousand'
1234567890: 'one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety'
12345678901: 'twelve billion three hundred forty-five million six hundred seventy-eight thousand nine hundred one'
123456789012: 'one hundred twenty-three billion four hundred fifty-six million seven hundred eighty-nine thousand twelve'
210987654321: 'two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000: 'one trillion'
1000000000001: 'one trillion one'
1000000001000: 'one trillion one thousand'
1000001000000: 'one trillion one million'
1000001001000: 'one trillion one million one thousand'
1001001001001: 'one trillion one billion one million one thousand one'
1234567890123: 'one trillion two hundred thirty-four billion five hundred sixty-seven million eight hundred ninety thousand one hundred twenty-three'
12345678901234: 'twelve trillion three hundred forty-five billion six hundred seventy-eight million nine hundred one thousand two hundred thirty-four'
123456789012345: 'one hundred twenty-three trillion four hundred fifty-six billion seven hundred eighty-nine million twelve thousand three hundred forty-five'
543210987654321: 'five hundred forty-three trillion two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000000: 'one quadrillion'
1000000000000001: 'one quadrillion one'
1010101010101010: 'one quadrillion ten trillion one hundred one billion ten million one hundred one thousand ten'
1234567890123456: 'one quadrillion two hundred thirty-four trillion five hundred sixty-seven billion eight hundred ninety million one hundred twenty-three thousand four hundred fifty-six'
6543210987654321: 'six quadrillion five hundred forty-three trillion two hundred ten billion nine hundred eighty-seven million six hundred fifty-four thousand three hundred twenty-one'
1000000000000000000: 'one quintillion'
1000000000000000000000: 'one sextillion'</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="text">number_name := n -> convert(n, english)
number_name(2001);
"two thousand one"</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="text">small = "zero"["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
Line 3,155 ⟶ 4,086:
Riffle[Select[
MapThread[StringJoin, {name /@ #, Reverse@big[[;; Length@#]]}] &@
IntegerDigits[n, 1000], StringFreeQ[#, "zero"] &], ","];</langsyntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
<lang Maxima>
l: [99, 300, 310, 1501, 12609, 512609, 43112609, 77000112609, 2000000000100,
999999999999999999, 0, -99, -1501, -77000112609, -123456789987654321];
map( lambda([n], printf(true, "~20d ~r~%", n, n)), l)$
</syntaxhighlight>
</lang>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
fn numToEng num =
(
Line 3,202 ⟶ 4,133:
ret = (toupper ret[1]) + (tolower (substring ret 2 -1)) -- make the first char uppercase and rest lowercase
return ret
)</langsyntaxhighlight>
 
Examples:
<syntaxhighlight lang="maxscript">
<lang MAXScript>
numtoeng 0
"Zero"
Line 3,221 ⟶ 4,152:
"Four hundred and twenty one million, seven hundred and fifty two thousand, three hundred and two"
 
</syntaxhighlight>
</lang>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">singles = " one two three four five six seven eight nine ".split
teens = "ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ".split
tys = " twenty thirty forty fifty sixty seventy eighty ninety".split
Line 3,251 ⟶ 4,182:
for n in [-1234, 0, 7, 42, 4325, 1000004, 214837564]
print n + ": " + numberName(n)
end for</langsyntaxhighlight>
 
Output:
Line 3,264 ⟶ 4,195:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import strutils, algorithm
 
const
Line 3,276 ⟶ 4,207:
"decillion"]
 
# Forward reference.
proc spellInteger(n: int64): string
 
proc nonzero(c: string,; n: int,int64; connect = ""): string =
if n == 0: "" else: connect & c & spellInteger(n)
 
proc lastAnd(num: string): string =
varif num',' =in num:
if "," inlet pos = num:.rfind(',')
letvar (pre, last) = if pos >= 0: (num[0 .rfind(".< pos]," num[pos+1 .. num.high])
var else: (pre"", lastnum) =
if " ifand pos" >=notin 0last: (num[0last ..= pos-1]," num[pos+1and" ..& num.high])last
result = else:[pre, (",", numlast].join()
else:
if " and " notin last:
lastresult = " and" & lastnum
num = [pre, ",", last].join()
return num
 
proc big(e, n: int64): string =
if e == 0:
spellInteger(n)
Line 3,301 ⟶ 4,231:
spellInteger(n) & " " & huge[e]
 
iterator base1000Rev(n: int64): intint64 =
var n = n
while n != 0:
Line 3,325 ⟶ 4,255:
var e = 0
for x in base1000Rev(n):
if x > 0: sq.add big(e, x)
sq.add big(e, x)
inc e
reverse sq
Line 3,337 ⟶ 4,266:
while n != 0:
echo align($n, 14)," -> ",spellInteger(n)
n = n div -10</langsyntaxhighlight>
 
Output:
{{out}}
<pre> 0 -> zero
-3 -> minus three
Line 3,364 ⟶ 4,294:
=={{header|Objeck}}==
{{trans|BASIC}}
<langsyntaxhighlight lang="objeck">
class NumberNames {
small : static : String[];
Line 3,458 ⟶ 4,388:
}
}
</syntaxhighlight>
</lang>
 
output:
Line 3,469 ⟶ 4,399:
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main() {
Line 3,484 ⟶ 4,414:
}
return 0;
}</langsyntaxhighlight>
Output:
<pre>
Line 3,496 ⟶ 4,426:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let div_mod n d = (n / d, n mod d)
let join = String.concat ", " ;;
 
Line 3,552 ⟶ 4,482:
in
join(List.map big fsegn)
;;</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">Eng(n:int)={
my(tmp,s="");
if (n >= 1000000,
Line 3,588 ⟶ 4,518:
Edigit(n)={
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][n]
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<langsyntaxhighlight lang="pascal">program NumberNames(output);
 
const
Line 3,673 ⟶ 4,603:
writeln(2343454564356, ': ', spell(2343454564356));
writeln(2345286538456328, ': ', spell(2345286538456328));
end.</langsyntaxhighlight>
Output:
 
Line 3,700 ⟶ 4,630:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use Lingua::EN::Numbers 'num2en';
 
print num2en(123456789), "\n";</langsyntaxhighlight>
 
=={{header|Perl 6}}==
Apart from the <tt>$m++</tt> this can be viewed as a purely functional program; we use nested <tt>gather</tt>/<tt>take</tt> constructs to avoid accumulators.
<lang perl6>constant @I = <zero one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen>;
constant @X = <0 X twenty thirty forty fifty sixty seventy eighty ninety>;
constant @C = @I X~ ' hundred';
constant @M = (<0 thousand>,
((<m b tr quadr quint sext sept oct non>,
(map { ('', <un duo tre quattuor quin sex septen octo novem>).flat X~ $_ },
<dec vigint trigint quadragint quinquagint sexagint septuagint octogint nonagint>),
'cent').flat X~ 'illion')).flat;
 
sub int-name ($num) {
if $num.substr(0,1) eq '-' { return "negative {int-name($num.substr(1))}" }
if $num eq '0' { return @I[0] }
my $m = 0;
return join ', ', reverse gather for $num.flip.comb(/\d ** 1..3/) {
my ($i,$x,$c) = .comb».Int;
if $i or $x or $c {
take join ' ', gather {
if $c { take @C[$c] }
if $x and $x == 1 { take @I[$i+10] }
else {
if $x { take @X[$x] }
if $i { take @I[$i] }
}
take @M[$m] // die "WOW! ZILLIONS!\n" if $m;
}
}
$m++;
}
}
 
while '' ne (my $n = prompt("Number: ")) {
say int-name($n);
}</lang>
Output:
<pre>Number: 0
zero
Number: 17
seventeen
Number: -1,234,567,890
negative one billion, two hundred thirty four million, five hundred sixty seven thousand, eight hundred ninety
Number: 42 000
forty two thousand
Number: 1001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001
one novemseptuagintillion, one octoseptuagintillion, one septenseptuagintillion, one sexseptuagintillion, one quinseptuagintillion, one quattuorseptuagintillion, one treseptuagintillion, one duoseptuagintillion, one unseptuagintillion, one septuagintillion, one novemsexagintillion, one octosexagintillion, one septensexagintillion, one sexsexagintillion, one quinsexagintillion, one quattuorsexagintillion, one tresexagintillion, one duosexagintillion, one unsexagintillion, one sexagintillion, one novemquinquagintillion, one octoquinquagintillion, one septenquinquagintillion, one sexquinquagintillion, one quinquinquagintillion, one quattuorquinquagintillion, one trequinquagintillion, one duoquinquagintillion, one unquinquagintillion, one quinquagintillion, one novemquadragintillion, one octoquadragintillion, one septenquadragintillion, one sexquadragintillion, one quinquadragintillion, one quattuorquadragintillion, one trequadragintillion, one duoquadragintillion, one unquadragintillion, one quadragintillion, one novemtrigintillion, one octotrigintillion, one septentrigintillion, one sextrigintillion, one quintrigintillion, one quattuortrigintillion, one tretrigintillion, one duotrigintillion, one untrigintillion, one trigintillion, one novemvigintillion, one octovigintillion, one septenvigintillion, one sexvigintillion, one quinvigintillion, one quattuorvigintillion, one trevigintillion, one duovigintillion, one unvigintillion, one vigintillion, one novemdecillion, one octodecillion, one septendecillion, one sexdecillion, one quindecillion, one quattuordecillion, one tredecillion, one duodecillion, one undecillion, one decillion, one nonillion, one octillion, one septillion, one sextillion, one quintillion, one quadrillion, one trillion, one billion, one million, one thousand, one
Number: 198723483017417
one hundred ninety eight trillion, seven hundred twenty three billion, four hundred eighty three million, seventeen thousand, four hundred seventeen</pre>
 
Alternately, we could use the Lingua::EN::Numbers module from the Perl 6 ecosystem. It will return similar output for similar inputs as above, but also handles fractions with configurable reduction and denominator, exponential notation, and ordinal notation.
 
<lang perl6>use Lingua::EN::Numbers; # Version 2.4.0 or higher
 
put join "\n", .&cardinal, .&cardinal(:improper) with -7/4;
 
printf "%-7s : %19s : %s\n", $_, cardinal($_), cardinal($_, :denominator(16)) for 1/16, 2/16 ... 1;
 
put join "\n", .&cardinal, .&cardinal-year, .&ordinal, .&ordinal-digit with 1999;
 
.&cardinal.put for 6.022e23, 42000, π;</lang>
 
<pre>negative one and three quarters
negative seven quarters
0.0625 : one sixteenth : one sixteenth
0.125 : one eighth : two sixteenths
0.1875 : three sixteenths : three sixteenths
0.25 : one quarter : four sixteenths
0.3125 : five sixteenths : five sixteenths
0.375 : three eighths : six sixteenths
0.4375 : seven sixteenths : seven sixteenths
0.5 : one half : eight sixteenths
0.5625 : nine sixteenths : nine sixteenths
0.625 : five eighths : ten sixteenths
0.6875 : eleven sixteenths : eleven sixteenths
0.75 : three quarters : twelve sixteenths
0.8125 : thirteen sixteenths : thirteen sixteenths
0.875 : seven eighths : fourteen sixteenths
0.9375 : fifteen sixteenths : fifteen sixteenths
1 : one : one
one thousand, nine hundred ninety-nine
nineteen ninety-nine
one thousand, nine hundred ninety-ninth
1999th
six point zero two two times ten to the twenty-third
forty-two thousand
three point one four one five nine two six five three five eight nine seven nine</pre>
 
=={{header|Phix}}==
{{Trans|Ada}}
Fraction ideas copied from [[Number_names#HicEst|HicEst]], using billion=10^9, trillion=10^12, quadrillion=10^15, and limited to 999 quadrillion.<br>
Implemented as an [[Executable_library]] for use in [[Names_to_numbers#Phix|Names_to_numbers]].<br>
The distribution now contains builtins\ordinal.e which implements spell() as ordinal(atom n, bool bJustSpell=true), see the manual.
<lang Phix>--
<!--<syntaxhighlight lang="phix">(phixonline)-->
-- demo/rosetta/Number_names.exw
<span style="color: #000080;font-style:italic;">--
--
-- demo\rosetta\Number_names.exw
constant twenties = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
-- -----------------------------
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">twenties</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"zero"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"four"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"six"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seven"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eight"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nine"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten"</span><span style="color: #0000FF;">,</span>
function Twenty(integer N)
<span style="color: #008000;">"eleven"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"twelve"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"thirteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fourteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fifteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sixteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seventeen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eighteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nineteen"</span><span style="color: #0000FF;">}</span>
return twenties[mod(N,20)+1]
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">twenty</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">decades</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"twenty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"thirty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"forty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fifty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sixty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seventy"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eighty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ninety"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decade</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">decades</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">twenty</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">decade</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">decade</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">'-'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">twenty</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">withand</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">withand</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">withand</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">twenty</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))&</span><span style="color: #008000;">" hundred"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">twenty</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">" hundred and "</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">orders</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"quadrillion"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"trillion"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"billion"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"million"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"thousand"</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">triplet</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">orders</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #004080;">atom</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">orders</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">high</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">order</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">low</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">high</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">high</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">&</span><span style="color: #000000;">name</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">low</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">low</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">high</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">", "</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"and "</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">1e-6</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" point"</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10.0000001</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">' '</span><span style="color: #0000FF;">&</span><span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">-</span><span style="color: #000000;">t</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">1e-6</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"minus "</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">triplet</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">samples</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">99</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">300</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">310</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">417</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1_501</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12_609</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">200000000000100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">999999999999999</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">123456787654321</span><span style="color: #0000FF;">,</span><span style="color: #000000;">102003000400005</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1020030004</span><span style="color: #0000FF;">,</span><span style="color: #000000;">102003</span><span style="color: #0000FF;">,</span><span style="color: #000000;">102</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">99</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">-</span><span style="color: #000000;">1501</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1234</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12.34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000001.2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1E-3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2.7182818</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">201021002001</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">20102100200</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2010210020</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">201021002</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20102100</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2010210</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">201021</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">20102</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2010</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">201</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">global</span>
constant decades = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
<span style="color: #008080;">function</span> <span style="color: #000000;">smartp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%18.8f"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">samples</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">samples</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%18s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">smartp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">),</span><span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">include_file</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
function Decade(integer N)
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
return decades[mod(N,10)-1]
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
function Hundred(integer N)
if N<20 then
return Twenty(N)
elsif mod(N,10)=0 then
return Decade(mod(floor(N/10),10))
end if
return Decade(floor(N/10)) & '-' & Twenty(mod(N,10))
end function
function Thousand(integer N, string withand)
if N<100 then
return withand & Hundred (N);
elsif mod(N,100)=0 then
return withand & Twenty(floor(N/100))&" hundred"
end if
return Twenty(floor(N/100)) & " hundred and " & Hundred(mod(N,100))
end function
constant orders = {{power(10,15),"quadrillion"},
{power(10,12),"trillion"},
{power(10,9),"billion"},
{power(10,6),"million"},
{power(10,3),"thousand"}}
function Triplet(atom N)
atom Order, High, Low
string Name, res = ""
integer n
for i=1 to length(orders) do
{Order,Name} = orders[i]
High = floor(N/Order)
Low = mod(N,Order)
if High!=0 then
res &= Thousand(High,"")&' '&Name
end if
N = Low
if Low=0 then exit end if
if length(res) and High!=0 then
res &= ", "
end if
end for
if N!=0 or res="" then
res &= Thousand(floor(N),iff(res=""?"":"and "))
N = abs(mod(N,1))
if N>1e-6 then
res &= " point"
for i=1 to 10 do
n = floor(N*10.0000001)
res &= ' '&twenties[n+1]
N = N*10-n
if abs(N)<1e-6 then exit end if
end for
end if
end if
return res
end function
global function spell(atom N)
string res = ""
if N<0 then
res = "minus "
N = -N
end if
res &= Triplet(N)
return res
end function
global
constant Samples = {99, 300, 310, 417,1_501, 12_609, 200000000000100, 999999999999999,
-123456787654321,102003000400005,1020030004,102003,102,1,0,-1,-99,
-1501,1234,12.34,10000001.2,1E-3,-2.7182818,
201021002001,-20102100200,2010210020,-201021002,20102100,-2010210,
201021,-20102,2010,-201,20,-2}
global function smartp(atom N)
string res
if N=floor(N) then return sprintf("%d",N) end if
res = sprintf("%18.8f",N)
if find('.',res) then
res = trim_tail(res,"0")
end if
return res
end function
procedure main()
atom si
for i=1 to length(Samples) do
si = Samples[i]
printf(1,"%18s %s\n",{smartp(si),spell(si)})
end for
end procedure
 
-- from Executable_library#Phix --
function isMainOrInclude()
-- returns 1 if called from the main file, 0 if from an include
integer res
#ilASM{
[32]
mov eax,[ebp+20] -- prev_ebp
mov eax,[eax+8] -- rtn
mov [res],eax
[64]
mov rax,[rbp+40] -- prev_ebp
mov rax,[rax+16] -- rtn
mov [res],rax
[]
}
return res=21 -- (21=T_maintls)
end function
 
if isMainOrInclude() then
main()
end if</lang>
{{out}}
<pre style="font-size: 8px">
Line 3,965 ⟶ 4,794:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$orderOfMag = array('Hundred', 'Thousand,', 'Million,', 'Billion,', 'Trillion,');
$smallNumbers = array('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
Line 4,021 ⟶ 4,850:
// The number is zero
return $str . "{$smallNumbers[(int)$thisPart]}";
}</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="php">NumberToEnglish(0);
NumberToEnglish(12);
NumberToEnglish(123);
NumberToEnglish(1234567890123);
NumberToEnglish(65535);
NumberToEnglish(-54321);</langsyntaxhighlight>
Returns:
<pre>Zero
Line 4,038 ⟶ 4,867:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de numName (N)
(cond
((=0 N) "zero")
Line 4,059 ⟶ 4,888:
(pack "-" (numNm (% N 10))) ) ) )
((rank N '((100 . "hundred") (1000 . "thousand") (1000000 . "million")))
(pack (numNm (/ N (car @))) " " (cdr @) " " (numNm (% N (car @)))) ) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii"> declare integer_names (0:20) character (9) varying static initial
('zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
Line 4,115 ⟶ 4,944:
value = value ||h || v || y(i);
end;
put skip edit (trim(N), ' = ', value) (a);</langsyntaxhighlight>
 
=={{header|PL/M}}==
The largest natively supported integer type is only 16 bits wide, so this program only
works up to 65535.
 
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
DECLARE FCB$NAME LITERALLY '5DH'; /* CP/M STORES COMMAND ARGUMENT HERE */
 
/* READ ASCII NUMBER */
READ$NUMBER: PROCEDURE (PTR) ADDRESS;
DECLARE PTR ADDRESS, C BASED PTR BYTE, RSLT ADDRESS;
RSLT = 0;
DO WHILE '0' <= C AND C <= '9';
RSLT = RSLT * 10 + C - '0';
PTR = PTR + 1;
END;
RETURN RSLT;
END;
 
/* SPELL 16-BIT NUMBER */
SPELL: PROCEDURE (N);
DECLARE N ADDRESS;
IF N=0 THEN DO;
CALL PRINT(.'ZERO$');
RETURN;
END;
SMALL: PROCEDURE (N) ADDRESS;
DECLARE N BYTE;
DO CASE N;
RETURN .'$'; RETURN .'ONE$'; RETURN .'TWO$';
RETURN .'THREE$'; RETURN .'FOUR$'; RETURN .'FIVE$';
RETURN .'SIX$'; RETURN .'SEVEN$'; RETURN .'EIGHT$';
RETURN .'NINE$'; RETURN .'TEN$'; RETURN .'ELEVEN$';
RETURN .'TWELVE$'; RETURN .'THIRTEEN$'; RETURN .'FOURTEEN$';
RETURN .'FIFTEEN$'; RETURN .'SIXTEEN$'; RETURN .'SEVENTEEN$';
RETURN .'EIGHTEEN$'; RETURN .'NINETEEN$';
END;
END SMALL;
TEENS: PROCEDURE (N) ADDRESS;
DECLARE N BYTE;
DO CASE N-2;
RETURN .'TWENTY$';
RETURN .'THIRTY$';
RETURN .'FORTY$';
RETURN .'FIFTY$';
RETURN .'SIXTY$';
RETURN .'SEVENTY$';
RETURN .'EIGHTY$';
RETURN .'NINETY$';
END;
END TEENS;
LESS$100: PROCEDURE (N);
DECLARE N BYTE;
IF N >= 20 THEN DO;
CALL PRINT(TEENS(N/10));
N = N MOD 10;
IF N > 0 THEN CALL PRINT(.'-$');
END;
CALL PRINT(SMALL(N));
END LESS$100;
LESS$1000: PROCEDURE (N);
DECLARE N ADDRESS;
IF N >= 100 THEN DO;
CALL LESS$100(N/100);
CALL PRINT(.' HUNDRED$');
N = N MOD 100;
IF N > 0 THEN CALL PRINT(.' $');
END;
CALL LESS$100(N);
END LESS$1000;
IF N >= 1000 THEN DO;
CALL LESS$1000(N/1000);
CALL PRINT(.' THOUSAND$');
N = N MOD 1000;
IF N > 0 THEN CALL PRINT(.' $');
END;
CALL LESS$1000(N);
END SPELL;
 
CALL SPELL(READ$NUMBER(FCB$NAME));
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>A>spell 0
ZERO
A>spell 65535
SIXTY-FIVE THOUSAND FIVE HUNDRED THIRTY-FIVE
A>spell 1212
ONE THOUSAND TWO HUNDRED TWELVE</pre>
 
=={{header|PowerBASIC}}==
Line 4,122 ⟶ 5,044:
Note that the PB compiler has some limitations related to how the <CODE>QUAD</CODE> data type is handled behind the scenes (extremely large values lose precision; see the sample output below the code).
 
<langsyntaxhighlight lang="powerbasic">FUNCTION int2Text (number AS QUAD) AS STRING
IF 0 = number THEN
FUNCTION = "zero"
Line 4,189 ⟶ 5,111:
#ENDIF
? int2Text(n)
END FUNCTION</langsyntaxhighlight>
 
Sample output:
Line 4,198 ⟶ 5,120:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-NumberName
{
Line 4,330 ⟶ 5,252:
 
1, 234, 31337, 987654321 | Get-NumberName
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 4,342 ⟶ 5,264:
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">
:- module(spell, [spell/2]).
 
Line 4,404 ⟶ 5,326:
spell(N, Rest),
string_concat(", ", Rest, S).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 4,426 ⟶ 5,348:
=={{header|PureBasic}}==
The range of integers handled has been set at an obscene 45 digits.
<langsyntaxhighlight PureBasiclang="purebasic">DataSection
numberNames:
;small
Line 4,546 ⟶ 5,468:
CloseConsole()
EndIf
</syntaxhighlight>
</lang>
{{out}}
<pre>Give me an integer (or q to quit)! 3
Line 4,557 ⟶ 5,479:
one hundred tredecillion, two decillion, three hundred quadrillion, four</pre>
 
== {{header|Python}} ==
Note: This example is also used as a module in the [[Names to numbers#Python]] task and should be kept in-sync.
 
<langsyntaxhighlight lang="python">TENS = [None, None, "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"]
SMALL = ["zero", "one", "two", "three", "four", "five",
Line 4,623 ⟶ 5,545:
n //= -10
print('%-12i -> %s' % (n, spell_integer(n)))
print('')</langsyntaxhighlight>
 
{{out}}
Line 4,653 ⟶ 5,575:
An alternative solution that can name very large numbers.
 
<langsyntaxhighlight lang="python">
def int_to_english(n):
if n < 0: return "minus " + int_to_english(-n)
Line 4,703 ⟶ 5,625:
print(int_to_english(2 ** 100))
print(int_to_english(10 ** (2*64)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,711 ⟶ 5,633:
one vigintillion vigintillion
</pre>
 
=={{header|Quackery}}==
 
{{trans|C#}}
 
<syntaxhighlight lang="quackery">[ [ table
$ "zero" $ "one" $ "two"
$ "three" $ "four" $ "five"
$ "six" $ "seven" $ "eight"
$ "nine" $ "ten" $ "eleven"
$ "twelve" $ "thirteen"
$ "fourteen" $ "fifteen"
$ "sixteen" $ "seventeen"
$ "eighteen" $ "nineteen" ] do ] is units ( n --> $ )
 
[ [ table
$ "nonety" $ "tenty" $ "twenty"
$ "thirty" $ "fourty" $ "fifty"
$ "sixty" $ "seventy" $ "eighty"
$ "ninety" ] do ] is tens ( n --> $ )
 
[ $ "" swap
dup 99 > if
[ 100 /mod swap units
$ " hundred" join
swap dip join
dup 0 = iff drop ]done[ ]
over size 0 > if
[ dip [ $ " and " join ] ]
dup 19 > if
[ 10 /mod swap tens
swap dip join
dup 0 = iff drop ]done[ ]
over size 0 > if
[ over -1 peek space != if
[ dip [ space join ] ] ]
units join ] is triplet ( n --> $ )
 
[ $ "" swap
dup 999999999999999 > if
[ 1000000000000000 /mod swap triplet
$ " quadrillion" join
swap dip join
dup 0 = iff drop ]done[ ]
dup 999999999999 > if
[ over size 0 > if
[ dip [ $ ", " join ] ]
1000000000000 /mod swap triplet
$ " trillion" join
swap dip join
dup 0 = iff drop ]done[ ]
dup 999999999 > if
[ over size 0 > if
[ dip [ $ ", " join ] ]
1000000000 /mod swap triplet
$ " billion" join
swap dip join
dup 0 = iff drop ]done[ ]
dup 999999 > if
[ over size 0 > if
[ dip [ $ ", " join ] ]
1000000 /mod swap triplet
$ " million" join
swap dip join
dup 0 = iff drop ]done[ ]
dup 999 > if
[ over size 0 > if
[ dip [ $ ", " join ] ]
1000 /mod swap triplet
$ " thousand" join
swap dip join
dup 0 = iff drop ]done[ ]
over size 0 > if
[ dip [ $ ", " join ] ]
triplet join
dup reverse witheach
[ char , = if
[ i split
behead drop
$ " and" swap
join join
conclude ] ] ] is name$ ( n --> $ )
 
10 times
[ 10 18 random
1+ ** random
dup echo
say " is:"
name$ nest$
60 wrap$ cr cr ]</syntaxhighlight>
 
{{out}}
 
<pre>2741791 is:
two million, seven hundred, fourty one thousand, seven
hundred and ninety one
 
4 is:
four
 
63798714 is:
sixty three million, seven hundred, ninety eight thousand,
seven hundred and fourteen
 
777100851236 is:
seven hundred, seventy seven billion, one hundred million,
eight hundred, fifty one thousand, two hundred and thirty
six
 
3689199513 is:
three billion, six hundred, eighty nine million, one
hundred, ninety nine thousand, five hundred and thirteen
 
703514386370 is:
seven hundred, three billion, five hundred, fourteen
million, three hundred, eighty six thousand, three hundred
and seventy
 
21545842 is:
twenty one million, five hundred, fourty five thousand,
eight hundred and fourty two
 
3867 is:
three thousand, eight hundred and sixty seven
 
4902020 is:
four million, nine hundred, two thousand and twenty
 
47022976290599343 is:
fourty seven quadrillion, twenty two trillion, nine hundred,
seventy six billion, two hundred, ninety million, five
hundred, ninety nine thousand, three hundred and fourty
three</pre>
 
=={{header|R}}==
Can do zero and negatives.
<langsyntaxhighlight Rlang="r"># Number Names
 
ones <- c("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
Line 4,765 ⟶ 5,820:
my_num$text <- as.character(lapply(my_num$nums, make_number))
print(my_num, right=F)
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,810 ⟶ 5,865:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 4,848 ⟶ 5,903:
(define r (+ (* e (random e)) (random e)))
(printf "~s: ~a\n" r (integer->english r)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,865 ⟶ 5,920:
 
See also [http://planet.racket-lang.org/package-source/neil/numspell.plt/1/2/planet-docs/numspell/index.html numspell] by Neil van Dyke.
 
=={{header|Raku}}==
(formerly Perl 6)
 
Apart from the <tt>$m++</tt> this can be viewed as a purely functional program; we use nested <tt>gather</tt>/<tt>take</tt> constructs to avoid accumulators.
<syntaxhighlight lang="raku" line>constant @I = <zero one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen>;
constant @X = <0 X twenty thirty forty fifty sixty seventy eighty ninety>;
constant @C = @I X~ ' hundred';
constant @M = (<0 thousand>,
((<m b tr quadr quint sext sept oct non>,
(map { ('', <un duo tre quattuor quin sex septen octo novem>).flat X~ $_ },
<dec vigint trigint quadragint quinquagint sexagint septuagint octogint nonagint>),
'cent').flat X~ 'illion')).flat;
 
sub int-name ($num) {
if $num.substr(0,1) eq '-' { return "negative {int-name($num.substr(1))}" }
if $num eq '0' { return @I[0] }
my $m = 0;
return join ', ', reverse gather for $num.flip.comb(/\d ** 1..3/) {
my ($i,$x,$c) = .comb».Int;
if $i or $x or $c {
take join ' ', gather {
if $c { take @C[$c] }
if $x and $x == 1 { take @I[$i+10] }
else {
if $x { take @X[$x] }
if $i { take @I[$i] }
}
take @M[$m] // die "WOW! ZILLIONS!\n" if $m;
}
}
$m++;
}
}
 
while '' ne (my $n = prompt("Number: ")) {
say int-name($n);
}</syntaxhighlight>
Output:
<pre>Number: 0
zero
Number: 17
seventeen
Number: -1,234,567,890
negative one billion, two hundred thirty four million, five hundred sixty seven thousand, eight hundred ninety
Number: 42 000
forty two thousand
Number: 1001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001001
one novemseptuagintillion, one octoseptuagintillion, one septenseptuagintillion, one sexseptuagintillion, one quinseptuagintillion, one quattuorseptuagintillion, one treseptuagintillion, one duoseptuagintillion, one unseptuagintillion, one septuagintillion, one novemsexagintillion, one octosexagintillion, one septensexagintillion, one sexsexagintillion, one quinsexagintillion, one quattuorsexagintillion, one tresexagintillion, one duosexagintillion, one unsexagintillion, one sexagintillion, one novemquinquagintillion, one octoquinquagintillion, one septenquinquagintillion, one sexquinquagintillion, one quinquinquagintillion, one quattuorquinquagintillion, one trequinquagintillion, one duoquinquagintillion, one unquinquagintillion, one quinquagintillion, one novemquadragintillion, one octoquadragintillion, one septenquadragintillion, one sexquadragintillion, one quinquadragintillion, one quattuorquadragintillion, one trequadragintillion, one duoquadragintillion, one unquadragintillion, one quadragintillion, one novemtrigintillion, one octotrigintillion, one septentrigintillion, one sextrigintillion, one quintrigintillion, one quattuortrigintillion, one tretrigintillion, one duotrigintillion, one untrigintillion, one trigintillion, one novemvigintillion, one octovigintillion, one septenvigintillion, one sexvigintillion, one quinvigintillion, one quattuorvigintillion, one trevigintillion, one duovigintillion, one unvigintillion, one vigintillion, one novemdecillion, one octodecillion, one septendecillion, one sexdecillion, one quindecillion, one quattuordecillion, one tredecillion, one duodecillion, one undecillion, one decillion, one nonillion, one octillion, one septillion, one sextillion, one quintillion, one quadrillion, one trillion, one billion, one million, one thousand, one
Number: 198723483017417
one hundred ninety eight trillion, seven hundred twenty three billion, four hundred eighty three million, seventeen thousand, four hundred seventeen</pre>
 
Alternately, we could use the [https://modules.raku.org/search/?q=Lingua%3A%3AEN%3A%3ANumbers Lingua::EN::Numbers module from the Raku ecosystem]. It will return similar output for similar inputs as above, but also handles fractions with configurable reduction and denominator, exponential notation, and ordinal notation.
 
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; # Version 2.4.0 or higher
 
put join "\n", .&cardinal, .&cardinal(:improper) with -7/4;
 
printf "%-7s : %19s : %s\n", $_, cardinal($_), cardinal($_, :denominator(16)) for 1/16, 2/16 ... 1;
 
put join "\n", .&cardinal, .&cardinal-year, .&ordinal, .&ordinal-digit with 1999;
 
.&cardinal.put for 6.022e23, 42000, π;</syntaxhighlight>
 
<pre>negative one and three quarters
negative seven quarters
0.0625 : one sixteenth : one sixteenth
0.125 : one eighth : two sixteenths
0.1875 : three sixteenths : three sixteenths
0.25 : one quarter : four sixteenths
0.3125 : five sixteenths : five sixteenths
0.375 : three eighths : six sixteenths
0.4375 : seven sixteenths : seven sixteenths
0.5 : one half : eight sixteenths
0.5625 : nine sixteenths : nine sixteenths
0.625 : five eighths : ten sixteenths
0.6875 : eleven sixteenths : eleven sixteenths
0.75 : three quarters : twelve sixteenths
0.8125 : thirteen sixteenths : thirteen sixteenths
0.875 : seven eighths : fourteen sixteenths
0.9375 : fifteen sixteenths : fifteen sixteenths
1 : one : one
one thousand, nine hundred ninety-nine
nineteen ninety-nine
one thousand, nine hundred ninety-ninth
1999th
six point zero two two times ten to the twenty-third
forty-two thousand
three point one four one five nine two six five three five eight nine seven nine</pre>
 
=={{header|REXX}}==
Line 4,876 ⟶ 6,021:
this simple script available [https://github.com/AbdelrahmanGIT/RingSamples/blob/master/src/ConvertNumbersToString.ring here]
 
<langsyntaxhighlight lang="ring">
OneList=["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
Line 4,953 ⟶ 6,098:
return str2list(str)
 
</syntaxhighlight>
</lang>
{{out}}
for input of:
Line 5,021 ⟶ 6,166:
{{works with|Ruby|1.9.2+}}
 
<langsyntaxhighlight lang="ruby">SMALL = %w(zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen
eighteen nineteen)
Line 5,080 ⟶ 6,225:
puts "Error: #{e}"
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 5,104 ⟶ 6,249:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::io::{self, Write, stdout};
 
const SMALL: &[&str] = &[
Line 5,174 ⟶ 6,319:
write!(&mut stdout, "\n").unwrap();
}
}</langsyntaxhighlight>
 
{{out}}
Line 5,189 ⟶ 6,334:
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight lang="scala">import scala.annotation.tailrec
import scala.collection.parallel.ParSeq
 
Line 5,294 ⟶ 6,439:
 
}.seq.sorted.foreach(num => println(f"$num%+,80d -> ${longhand(numeral = num, showAnd = true)}"))
} // object SpellNumber @ line 110</langsyntaxhighlight>
 
{{out}}
Line 5,405 ⟶ 6,550:
=== Recursive ===
Recursive TreeMap solution (for values up to trillions):
<langsyntaxhighlight lang="scala">import scala.collection.immutable.TreeMap
 
val NUMBERS = TreeMap(
Line 5,431 ⟶ 6,576:
}
}
</syntaxhighlight>
</lang>
 
Examples
Line 5,445 ⟶ 6,590:
The library [http://seed7.sourceforge.net/libraries/wrinum.htm wrinum.s7i] contains the function [http://seed7.sourceforge.net/libraries/wrinum.htm#str%28ENGLISH,in_integer%29 str(ENGLISH, ...)] which converts an integer to its written english equivalent.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "stdio.s7i";
include "wrinum.s7i";
Line 5,453 ⟶ 6,598:
var integer: number is 0;
begin
for number range 1 to 9999991000000 do
writeln(str(ENGLISH, number));
end for;
end func;</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">
set numbers to [0,1,7,22,186,pi,-48.6,-3451,925734, 12570902378]
 
Line 5,467 ⟶ 6,612:
put output
end repeat
</syntaxhighlight>
</lang>
Output:
<pre>
Line 5,484 ⟶ 6,629:
=={{header|SequenceL}}==
Works on all 32 bit signed integers.<br>
<langsyntaxhighlight lang="sequencel">import <Utilities/Math.sl>;
import <Utilities/Sequence.sl>;
import <Utilities/Conversion.sl>;
Line 5,544 ⟶ 6,689:
in
hundredsWord ++ andWord ++ tensWord ++ onesWord;</langsyntaxhighlight>
 
{{out}}
Line 5,558 ⟶ 6,703:
one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine"
</pre>
 
=={{header|Shale}}==
 
<syntaxhighlight lang="shale">#!/usr/local/bin/shale
 
maths library
 
0 ones:: dup var "zero" =
1 ones:: dup var "one" =
2 ones:: dup var "two" =
3 ones:: dup var "three" =
4 ones:: dup var "four" =
5 ones:: dup var "five" =
6 ones:: dup var "six" =
7 ones:: dup var "seven" =
8 ones:: dup var "eight" =
9 ones:: dup var "nine" =
10 teens:: dup var "ten" =
11 teens:: dup var "eleven" =
12 teens:: dup var "twelve" =
13 teens:: dup var "thirteen" =
14 teens:: dup var "fourteen" =
15 teens:: dup var "fifteen" =
16 teens:: dup var "sixteen" =
17 teens:: dup var "seventeen" =
18 teens:: dup var "eighteen" =
19 teens:: dup var "nineteen" =
2 tens:: dup var "twenty" =
3 tens:: dup var "thirty" =
4 tens:: dup var "forty" =
5 tens:: dup var "fifty" =
6 tens:: dup var "sixty" =
7 tens:: dup var "seventy" =
8 tens:: dup var "eighty" =
9 tens:: dup var "ninety" =
 
say dup var {
commaFlag dup var swap =
andFlag dup var swap =
n dup var swap =
h dup var n 100 / =
t dup var n 100 % =
o dup var n 10 % =
 
h 0 > {
commaFlag { "," print commaFlag false = } ifthen
h.value ones:: " %s hundred" printf
} ifthen
 
h 0 > t 0 > and t 0 > andFlag and or { " and" print commaFlag false = } ifthen
 
t 9 > {
t 19 > {
commaFlag { "," print commaFlag false = } ifthen
t 10 / tens:: " %s" printf
o 0 > { o.value ones:: "-%s" printf } ifthen
} {
t 9 > { commaFlag { "," print } ifthen t.value teens:: " %s" printf } ifthen
} if
} {
o 0 > { commaFlag { "," print } ifthen o.value ones:: " %s" printf } ifthen
} if
} =
 
speak dup var {
n dup var swap =
m dup var n 1000000 / =
t dup var n 1000 / 1000 % =
h dup var n 1000 % =
 
n "%10d ->" printf
m 0 > { m.value false false say() " million" print } ifthen
t 0 > { t.value false m 0 > say() " thousand" print } ifthen
h 0 > m 0 == t 0 == and or { h.value m 0 > t 0 > or dup say() } ifthen
"" println
} =
 
"Stock numbers" println
1 speak()
10 speak()
100 speak()
1000 speak()
1001 speak()
100001000 speak()
100001001 speak()
123456789 speak()
987654321 speak()
100200300 speak()
10020030 speak()
 
"" println
"Randomly generated numbers" println
i var
i 0 =
{ i 10 < } {
random maths::() 1000000000 % speak() // Cap it to less than 1 billion.
i++
} while</syntaxhighlight>
 
{{out}}
 
<pre>Stock numbers
1 -> one
10 -> ten
100 -> one hundred
1000 -> one thousand
1001 -> one thousand and one
100001000 -> one hundred million, one thousand
100001001 -> one hundred million, one thousand and one
123456789 -> one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine
987654321 -> nine hundred and eighty-seven million, six hundred and fifty-four thousand, three hundred and twenty-one
100200300 -> one hundred million, two hundred thousand, three hundred
10020030 -> ten million, twenty thousand and thirty
 
Randomly generated numbers
430442705 -> four hundred and thirty million, four hundred and forty-two thousand, seven hundred and five
542387280 -> five hundred and forty-two million, three hundred and eighty-seven thousand, two hundred and eighty
612564971 -> six hundred and twelve million, five hundred and sixty-four thousand, nine hundred and seventy-one
620405002 -> six hundred and twenty million, four hundred and five thousand and two
203302758 -> two hundred and three million, three hundred and two thousand, seven hundred and fifty-eight
39259420 -> thirty-nine million, two hundred and fifty-nine thousand, four hundred and twenty
911059261 -> nine hundred and eleven million, fifty-nine thousand, two hundred and sixty-one
370836312 -> three hundred and seventy million, eight hundred and thirty-six thousand, three hundred and twelve
293702626 -> two hundred and ninety-three million, seven hundred and two thousand, six hundred and twenty-six
124444645 -> one hundred and twenty-four million, four hundred and forty-four thousand, six hundred and forty-five</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var l = frequire('Lingua::EN::Numbers');
say l.num2en(123456789);</langsyntaxhighlight>
 
{{out}}
Line 5,569 ⟶ 6,839:
 
=={{header|SQL}}==
<syntaxhighlight lang="sql">
<lang SQL>
select val, to_char(to_date(val,'j'),'jsp') name
from
Line 5,580 ⟶ 6,850:
 
select to_char(to_date(5373485,'j'),'jsp') from dual;
</syntaxhighlight>
</lang>
<pre>
VAL NAME
Line 5,600 ⟶ 6,870:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">extension Int {
private static let bigNames = [
1_000: "thousand",
Line 5,733 ⟶ 7,003:
for number in nums {
print("\(number) => \(number.numberName)")
}</langsyntaxhighlight>
 
{{out}}
Line 5,755 ⟶ 7,025:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc int2words {n} {
if { ! [regexp -- {^(-?\d+)$} $n -> n]} {
error "not a decimal integer"
Line 5,826 ⟶ 7,096:
catch {int2words $test} result
puts "$test -> $result"
}</langsyntaxhighlight>
produces
<div style="width:full;overflow:scroll">
Line 5,849 ⟶ 7,119:
</pre>
</div>
 
=={{header|Visual Basic}}==
{{trans|BASIC}}
 
If one were to use variants further and get them to play nice as <code>Decimal</code>, this could theoretically be extended up to the octillion range.
<lang vb>Option Explicit
 
Private small As Variant, tens As Variant, big As Variant
 
Sub Main()
small = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", _
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", _
"eighteen", "nineteen")
tens = Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
big = Array("thousand", "million", "billion")
 
Dim tmpInt As Long
tmpInt = Val(InputBox("Gimme a number!", "NOW!", Trim$(Year(Now)) & IIf(Month(Now) < 10, "0", "") & _
Trim$(Month(Now)) & IIf(Day(Now) < 10, "0", "") & Trim$(Day(Now))))
MsgBox int2Text$(tmpInt)
End Sub
 
Function int2Text$(number As Long)
Dim num As Long, outP As String, unit As Integer
Dim tmpLng1 As Long
 
If 0 = number Then
int2Text$ = "zero"
Exit Function
End If
 
num = Abs(number)
 
Do
tmpLng1 = num Mod 100
Select Case tmpLng1
Case 1 To 19
outP = small(tmpLng1 - 1) + " " + outP
Case 20 To 99
Select Case tmpLng1 Mod 10
Case 0
outP = tens((tmpLng1 \ 10) - 2) + " " + outP
Case Else
outP = tens((tmpLng1 \ 10) - 2) + "-" + small(tmpLng1 Mod 10) + " " + outP
End Select
End Select
 
tmpLng1 = (num Mod 1000) \ 100
If tmpLng1 Then
outP = small(tmpLng1 - 1) + " hundred " + outP
End If
 
num = num \ 1000
If num < 1 Then Exit Do
 
tmpLng1 = num Mod 1000
If tmpLng1 Then outP = big(unit) + " " + outP
 
unit = unit + 1
Loop
 
If number < 0 Then outP = "negative " & outP
 
int2Text$ = Trim$(outP)
End Function</lang>
 
Example output (in a msgbox) is identical to the BASIC output.
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Public twenties As Variant
Public decades As Variant
Public orders As Variant
Line 6,031 ⟶ 7,234:
Debug.Print Format(smartp(si), "@@@@@@@@@@@@@@@@"); " "; spell(si)
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre> 99 ninety-nine
300 three hundred
Line 6,067 ⟶ 7,270:
20 twenty
-2 minus two</pre>
 
=={{header|Visual Basic}}==
{{trans|BASIC}}
 
If one were to use variants further and get them to play nice as <code>Decimal</code>, this could theoretically be extended up to the octillion range.
<syntaxhighlight lang="vb">Option Explicit
 
Private small As Variant, tens As Variant, big As Variant
 
Sub Main()
small = Array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", _
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", _
"eighteen", "nineteen")
tens = Array("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
big = Array("thousand", "million", "billion")
 
Dim tmpInt As Long
tmpInt = Val(InputBox("Gimme a number!", "NOW!", Trim$(Year(Now)) & IIf(Month(Now) < 10, "0", "") & _
Trim$(Month(Now)) & IIf(Day(Now) < 10, "0", "") & Trim$(Day(Now))))
MsgBox int2Text$(tmpInt)
End Sub
 
Function int2Text$(number As Long)
Dim num As Long, outP As String, unit As Integer
Dim tmpLng1 As Long
 
If 0 = number Then
int2Text$ = "zero"
Exit Function
End If
 
num = Abs(number)
 
Do
tmpLng1 = num Mod 100
Select Case tmpLng1
Case 1 To 19
outP = small(tmpLng1 - 1) + " " + outP
Case 20 To 99
Select Case tmpLng1 Mod 10
Case 0
outP = tens((tmpLng1 \ 10) - 2) + " " + outP
Case Else
outP = tens((tmpLng1 \ 10) - 2) + "-" + small(tmpLng1 Mod 10) + " " + outP
End Select
End Select
 
tmpLng1 = (num Mod 1000) \ 100
If tmpLng1 Then
outP = small(tmpLng1 - 1) + " hundred " + outP
End If
 
num = num \ 1000
If num < 1 Then Exit Do
 
tmpLng1 = num Mod 1000
If tmpLng1 Then outP = big(unit) + " " + outP
 
unit = unit + 1
Loop
 
If number < 0 Then outP = "negative " & outP
 
int2Text$ = Trim$(outP)
End Function</syntaxhighlight>
 
Example output (in a msgbox) is identical to the BASIC output.
 
=={{header|Visual Basic .NET}}==
'''Platform:''' [[.NET]]
Line 6,074 ⟶ 7,345:
This solution works for integers up to 1000. It should be fairly ovbious how it works, and so can be extended if needed.
 
<langsyntaxhighlight lang="vbnet">Module Module1
Sub Main()
Line 6,101 ⟶ 7,372:
End Function
 
End Module</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">const (
small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
illions = ["", " thousand", " million", " billion", " trillion", " quadrillion", " quintillion"]
)
 
fn say(n i64) string {
mut t := ''
mut nn := n
if n < 0 {
t = "negative "
// Note, for math.MinInt64 this leaves n negative.
nn = -n
}
if nn < 20 {
t += small[nn]
}
else if nn < 100 {
t += tens[nn/10]
s := nn % 10
if s > 0 {
t += "-" + small[s]
}
}
else if nn < 1000 {
t += small[nn/100] + " hundred"
s := nn % 100
if s > 0 {
t += " " + say(s)
}
}
else {
// work right-to-left
mut sx := ""
for i := 0; nn > 0; i++ {
p := nn % 1000
nn /= 1000
if p > 0 {
mut ix := say(p) + illions[i]
if sx != "" {
ix += " " + sx
}
sx = ix
}
}
t += sx
}
return t
}
 
fn main(){
mut nums := []i64{}
nums = [i64(12), i64(1048576), i64(9e18), i64(-2), i64(0)]
for n in nums {
println(say(n))
}
}</syntaxhighlight>
{{out}}
<pre>
twelve
one million forty-eight thousand five hundred seventy-six
nine quintillion
negative two
zero
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
Although the third example here works correctly, it is not safe to use this script for numbers with an absolute magnitude >= 2^53 as integers cannot be expressed exactly by Wren's Num type beyond that limit.
<syntaxhighlight lang="wren">var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
 
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
 
var illions = ["", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion"]
 
var say
say = Fn.new { |n|
var t = ""
if (n < 0) {
t = "negative "
n = -n
}
if (n < 20) {
t = t + small[n]
} else if (n < 100) {
t = t + tens[(n/10).floor]
var s = n % 10
if (s > 0) t = t + "-" + small[s]
} else if (n < 1000) {
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
if (s > 0) t = t + " " + say.call(s)
} else {
var sx = ""
var i = 0
while (n > 0) {
var p = n % 1000
n = (n/1000).floor
if (p > 0) {
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
sx = ix
}
i = i + 1
}
t = t + sx
}
return t
}
 
for (n in [12, 1048576, 9e18, -2, 0]) System.print(say.call(n))</syntaxhighlight>
 
{{out}}
<pre>
twelve
one million forty-eight thousand five hundred seventy-six
nine quintillion
negative two
zero
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code ChOut=8, CrLf=9, Text=12;
 
proc NumName(Dev, Num); \Output integer Num in prose to device Dev
Line 6,156 ⟶ 7,551:
NumName(0, 1_000_000); CrLf(0);
NumName(0, 1_234_567_890); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 6,170 ⟶ 7,565:
=={{header|zkl}}==
{{trans|Scala}}
<langsyntaxhighlight lang="zkl">var
ns =[1..20].chain([30..90,10]).walk(),
names=T("one","two","three","four","five","six","seven","eight","nine",
Line 6,191 ⟶ 7,586:
}else ""
}(n).strip() // sometimes there is a trailing space
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in (T(85001,155019,4547000,6766027,55555555555)){
println("%,d is %s".fmt(n,numberToString(n)));
}</langsyntaxhighlight>
{{out}}
<pre>
1,980

edits