Number names: Difference between revisions
Added Easylang
m (Commenting Fōrmulæ solution, broken link) |
(Added Easylang) |
||
(17 intermediate revisions by 14 users not shown) | |||
Line 15:
=={{header|360 Assembly}}==
{{trans|AppleSoft Basic}}
<
NUMNAME CSECT
USING NUMNAME,R13
Line 197:
PG DS CL256
YREGS
END NUMNAME</
{{out}}
<pre>
Line 216:
=={{header|Ada}}==
<
procedure Integers_In_English is
Line 347:
Spell_And_Print(Samples(I));
end loop;
end Integers_In_English;</
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]}}
<
# returns a string representation of n in words. Currently
deals with anything from 0 to 999 999 999. #
Line 413:
OD;
stop iteration:
SKIP</
{{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]}}
<
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)</
{{out}}
<pre>
Line 530:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
small←'' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight'
small,←'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen'
Line 556:
}/orders,⊂(''⍵)),∇100|⍵
}⍵
}</
{{out}}
<pre> spell 0
Line 571:
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:
<
use framework "Foundation"
Line 579:
numberToWords(-3.6028797018963E+10)
--> "minus thirty-six billion twenty-eight million seven hundred ninety-seven thousand eighteen point nine six three"</
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.
Line 587:
Both scripts can display strange results with numbers at the extreme limits of floating-point resolution.
<
-- n: AppleScript integer or real.
-- longScale (optional): boolean. Whether to use long-scale -illions instead of short-scale. Default: false
Line 709:
--> "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"</
=={{header|Applesoft BASIC}}==
Handles zero and negative integers. Rounding errors occur with big numbers.
<
20 GOSUB 100"NUMBER NAME
30 PRINT R$
Line 747:
288 DATA "EIGHT", "EIGHTEEN", "EIGHTY", "OCTILLION"
289 DATA "NINE", "NINETEEN", "NINETY", "NONILLION"
290 DATA "", "", "", "DECILLION"</
=={{header|AutoHotkey}}==
<
n =
Random Digits, 1, 36 ; random number with up to 36 digits
Line 789:
PrettyNumber(n) { ; inserts thousands separators into a number string
Return RegExReplace( RegExReplace(n,"^0+(\d)","$1"), "\G\d+?(?=(\d{3})+(?:\D|$))", "$0,")
}</
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBER_NAMES.AWK
BEGIN {
Line 844:
split("ten twenty thirty forty fifty sixty seventy eighty ninety",tens," ")
}
</syntaxhighlight>
{{out}}
<pre>
Line 862:
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION int2Text$ (number AS LONG)
'small
Line 935 ⟶ 934:
int2Text$ = RTRIM$(outP)
END FUNCTION</
{{out|Sample outputs}} (including the answer to the ultimate question of life, the universe, and everything):
<pre> Gimme a number! 1
one
Gimme a number! 0
Line 952 ⟶ 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>
=={{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}}
<
::Batch File Implementation
Line 1,032 ⟶ 1,092:
set TotalOut=%TotalOut:~0,-1%
goto :EOF</
{{Out}}
<pre>>NUMBER.BAT
Line 1,048 ⟶ 1,108:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
test%() = 0, 1, 2, 19, 20, 21, 99, 100, 101, 300, 310, 1001, -1327, 1501, \
\ 10203, 12609, 101104, 102003, 467889, 1005006, -123000789
Line 1,087 ⟶ 1,147:
ENDIF
NEXT i%
= a$</
{{out}}
<pre>
Line 1,116 ⟶ 1,176:
This will work up to and including 999999, or up to the maximum of the system word size, whichever is smaller.
<
manifest $( MAXLEN = 256/BYTESPERWORD $)
Line 1,203 ⟶ 1,263:
writef("%N: %S*N", atoi(instr), spell(atoi(instr), numstr))
$) repeat
$)</
{{out}}
<pre>Number? 0
Line 1,221 ⟶ 1,281:
{{trans|C++}}
<
Framework BRL.StandardIO
Line 1,301 ⟶ 1,361:
Local numberSpell:TSpell = New TSpell
Print number + " " + numberSpell.spell(number)
End Function</
<pre>
99 ninety-nine
Line 1,314 ⟶ 1,374:
=={{header|C}}==
<
#include <string.h>
Line 1,450 ⟶ 1,510:
say_number("123456789012345678901234567890123456789012345678900000001");
return 0;
}</
{{out}}
<pre>minus forty-two
Line 1,462 ⟶ 1,522:
{{works with|C sharp|2.0+, works for numbers between 0 and 999,999,999}}
<
class NumberNamer {
Line 1,540 ⟶ 1,600:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,550 ⟶ 1,610:
=={{header|C++}}==
<
#include <iostream>
using std::string;
Line 1,618 ⟶ 1,678:
SPELL_IT(1234567890);
return 0;
}</
{{out}}
<pre>
Line 1,634 ⟶ 1,694:
{{trans|Common Lisp}}
<
=> "one thousand, two hundred thirty-four"</
=={{header|CoffeeScript}}==
{{trans|Python}}
<
spell_integer = (n) ->
tens = [null, null, "twenty", "thirty", "forty",
Line 1,698 ⟶ 1,758:
console.log spell_integer 2010
console.log spell_integer 4000123007913
</syntaxhighlight>
{{out}}
Line 1,726 ⟶ 1,786:
<
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,773 ⟶ 1,833:
1009 data nine,nineteen,ninety,octillion
1010 data "","","",nonillion
1011 data "","","",decillion</
{{out}}
Line 1,823 ⟶ 1,883:
=={{header|Common Lisp}}==
<
=> "one thousand two hundred thirty-four"</
=={{header|D}}==
{{trans|Python}}
use -version=number_names_main to see the output.
<
immutable tens = ["", "", "twenty", "thirty", "forty",
Line 1,906 ⟶ 1,966:
writeln;
}
}</
{{out}}
<pre> +0 -> zero
Line 1,934 ⟶ 1,994:
{{Trans|Pascal}}
Adaptation of [[#Pascal]] Program to run in delphi.
<syntaxhighlight lang="delphi">
program Number_names;
Line 2,021 ⟶ 2,081:
writeln(2345286538456328, ': ', spell(2345286538456328));
Readln;
end.</
=={{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}}
<
@small ~w(zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen
Line 2,072 ⟶ 2,290:
e in ArgumentError -> IO.puts Exception.message(e)
end
end)</
{{out}}
Line 2,098 ⟶ 2,316:
=={{header|Erlang}}==
<
-module(nr2eng).
-import(lists, [foreach/2, seq/2, append/2]).
Line 2,176 ⟶ 2,394:
end,
append(seq(1, 2000), [123123, 43234234])).
</syntaxhighlight>
{{out}}
<pre>
Line 2,187 ⟶ 2,405:
=={{header|Euphoria}}==
{{trans|BASIC}}
<
if i < 0 then
return -i
Line 2,258 ⟶ 2,476:
puts(1,int2text(1234567890) & "\n")
puts(1,int2text(-987654321) & "\n")
puts(1,int2text(0) & "\n")</
{{out}}
Line 2,268 ⟶ 2,486:
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<
let join = String.concat ", "
Line 2,325 ⟶ 2,543:
join (List.map big fsegn)
;;</
=={{header|Factor}}==
Factor "cheats" by having a standard library module for this task:
<
IN: scratchpad 43112609 number>text print
forty-three million, one hundred and twelve thousand, six hundred and nine
</syntaxhighlight>
=={{header|FreeBASIC}}==
Can handle floating point numbers, and negatives, with absolute value less than one billion.
<
"five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",_
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",_
Line 2,395 ⟶ 2,613:
print numname(987654)
print numname(100000017)
</syntaxhighlight>
{{out}}
<pre>
Line 2,408 ⟶ 2,626:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 2,474 ⟶ 2,692:
end do
end program spell</
Sample input:
<pre>-1
Line 2,487 ⟶ 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>.
<
import "fmt"
Line 2,557 ⟶ 2,812:
}
return t
}</
Output:
<pre>
Line 2,568 ⟶ 2,823:
=={{header|Groovy}}==
<
def qr = number.divideAndRemainder(divisor)
[div:qr[0], remainder:qr[1]]
Line 2,642 ⟶ 2,897:
verifyToText 'one million and forty five', 1000045
verifyToText 'one million and fifteen', 1000015
verifyToText 'one billion, forty five thousand and one', 1000045001</
Output:
<pre>twenty nine
Line 2,678 ⟶ 2,933:
=={{header|Haskell}}==
<
spellInteger :: Integer -> String
Line 2,713 ⟶ 2,968:
big (e, n) = spellInteger n ++ ' ' : (l !! e) ++ "illion"
where l = [undefined, undefined, "m", "b", "tr", "quadr",
"quint", "sext", "sept", "oct", "non", "dec"]</
=={{header|HicEst}}==
<
CHARACTER outP*255, small*130, tens*80, big*80
REAL :: decimal_places = 7
Line 2,785 ⟶ 3,040:
TENS=ten twenty thirty forty fifty sixty seventy eighty ninety
BIG=thousand million billion trillion quadrillion</
<
1234 = one thousand and two hundred thirty-four
12.34 = twelve point three four
Line 2,792 ⟶ 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</
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main(arglist)
every x := !arglist do
write(commas(x), " -> ",spell(x))
end</
{{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.
<
local m, i
static scale
Line 2,841 ⟶ 3,096:
}
else fail # really big
end</
Sample output:
<pre>#spell.exe 5 11 15 67 10132767 65535 -1234567890123456
Line 2,855 ⟶ 3,110:
=={{header|Inform 7}}==
{{works with|Z-machine}}
<syntaxhighlight lang
{{works with|Glulx virtual machine}}
<syntaxhighlight lang
=={{header|J}}==
'''Solutions:'''
<
v=. ;:'ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen'
t=. ;:'twenty thirty forty fifty sixty seventy eighty ninety'
Line 2,885 ⟶ 3,140:
uk=: ' and '&en NB. British
us=: ' ' &en NB. American</
'''Example:'''
Line 2,894 ⟶ 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}}==
<
;
Line 2,974 ⟶ 3,231:
return sb.toString().trim();
}
}</
Output:
<pre>zero
Line 2,993 ⟶ 3,250:
===Recursive===
<
final private static String[] units = {"Zero","One","Two","Three","Four",
Line 3,010 ⟶ 3,267:
return convert(i / 1000000) + " Million " + ((i % 1000000 > 0)? " " + convert(i % 1000000):"") ;
}
}</
=={{header|JavaScript}}==
{{trans|Groovy}}
<
const sayNumber = value => {
Line 3,057 ⟶ 3,314:
}
return name;
};</
=={{header|Joy}}==
<syntaxhighlight lang="joy">
DEFINE units ==
[
tens ==
[
convert6 ==
Line 3,104 ⟶ 3,361:
[convert2]
ifte.
</syntaxhighlight>
=={{header|jq}}==
<
# Tested with jq 1.4
#
Line 3,167 ⟶ 3,424:
end ;
say</
Transcript (input followed by output):
<
"zero"
-0
Line 3,185 ⟶ 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>
=={{header|Julia}}==
Line 3,191 ⟶ 3,448:
'''Number Names Functions'''
<
const teentext = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"]
Line 3,255 ⟶ 3,512:
n < toobig || return "too big to say"
digits2text!(digits(n, base=10), use_short_scale)
end</
'''Main'''
<
println("Some easy ones to start with\n")
Line 3,300 ⟶ 3,557:
println(" ", i, " is")
println(num2text(i, false))
</syntaxhighlight>
{{out}}
Line 3,395 ⟶ 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.
<
val oneNames = listOf(
Line 3,467 ⟶ 3,724:
println("Using UK representation:")
for (i in exampleNumbers) println("${"%20d".format(i)} = ${numToText(i, true)}")
}</
{{out}}
Line 3,509 ⟶ 3,766:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
global outnum$
dim ones$(20),tens$(9),gr$(5),group(5)
Line 3,619 ⟶ 3,876:
data "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
data "twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
</syntaxhighlight>
{{out}}
<pre>
Line 3,627 ⟶ 3,884:
=={{header|Logo}}==
<
eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen}
Line 3,654 ⟶ 3,911:
end
print to.english 1234567 ; one million two hundred thirty four thousand five hundred sixty seven</
=={{header|Lua}}==
===Original===
<
levels = {"thousand ", "million ", "billion ", "trillion ", "quadrillion ", "quintillion ", "sextillion ", "septillion ", "octillion ", [0] = ""}
iwords = {"ten ", "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "}
Line 3,694 ⟶ 3,951:
end
if #vword == 0 then print "zero" else print(vword) end</
===Alternate===
As used in [[Four is magic#Lua|Four is magic]]
<
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" }
Line 3,738 ⟶ 3,995:
for _, num in ipairs(numbers) do
print( string.format("%.f: '%s'", num, numname(num)) )
end</
{{out}}
<pre style="height:30ex;overflow:scroll">-1: 'negative one'
Line 3,801 ⟶ 4,058:
=={{header|Maple}}==
<syntaxhighlight lang="text">number_name := n -> convert(n, english)
number_name(2001);
"two thousand one"</
=={{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,829 ⟶ 4,086:
Riffle[Select[
MapThread[StringJoin, {name /@ #, Reverse@big[[;; Length@#]]}] &@
IntegerDigits[n, 1000], StringFreeQ[#, "zero"] &], ","];</
=={{header|Maxima}}==
<syntaxhighlight 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>
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
fn numToEng num =
(
Line 3,876 ⟶ 4,133:
ret = (toupper ret[1]) + (tolower (substring ret 2 -1)) -- make the first char uppercase and rest lowercase
return ret
)</
Examples:
<syntaxhighlight lang="maxscript">
numtoeng 0
"Zero"
Line 3,895 ⟶ 4,152:
"Four hundred and twenty one million, seven hundred and fifty two thousand, three hundred and two"
</syntaxhighlight>
=={{header|MiniScript}}==
<
teens = "ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen ".split
tys = " twenty thirty forty fifty sixty seventy eighty ninety".split
Line 3,925 ⟶ 4,182:
for n in [-1234, 0, 7, 42, 4325, 1000004, 214837564]
print n + ": " + numberName(n)
end for</
Output:
Line 3,938 ⟶ 4,195:
=={{header|Nim}}==
{{trans|Python}}
<
const
Line 4,009 ⟶ 4,266:
while n != 0:
echo align($n, 14)," -> ",spellInteger(n)
n = n div -10</
{{out}}
Line 4,037 ⟶ 4,294:
=={{header|Objeck}}==
{{trans|BASIC}}
<
class NumberNames {
small : static : String[];
Line 4,131 ⟶ 4,388:
}
}
</syntaxhighlight>
output:
Line 4,142 ⟶ 4,399:
=={{header|Objective-C}}==
<
int main() {
Line 4,157 ⟶ 4,414:
}
return 0;
}</
Output:
<pre>
Line 4,169 ⟶ 4,426:
=={{header|OCaml}}==
<
let join = String.concat ", " ;;
Line 4,225 ⟶ 4,482:
in
join(List.map big fsegn)
;;</
=={{header|PARI/GP}}==
<
my(tmp,s="");
if (n >= 1000000,
Line 4,261 ⟶ 4,518:
Edigit(n)={
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"][n]
};</
=={{header|Pascal}}==
{{works with|Free_Pascal}}
<
const
Line 4,346 ⟶ 4,603:
writeln(2343454564356, ': ', spell(2343454564356));
writeln(2345286538456328, ': ', spell(2345286538456328));
end.</
Output:
Line 4,373 ⟶ 4,630:
=={{header|Perl}}==
<
print num2en(123456789), "\n";</
=={{header|Phix}}==
Line 4,382 ⟶ 4,639:
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.
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Number_names.exw
-- -----------------------------
--</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>
<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>
<span style="color: #008080;">function</span> <span style="color: #000000;">
<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;">
<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;">
<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;">
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<span style="color: #008080;">return</span> <span style="color: #000000;">
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">
<span style="color: #008080;">return</span> <span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<span style="color: #008080;">return</span> <span style="color: #000000;">withand</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">
<span style="color: #008080;">return</span> <span style="color: #000000;">withand</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 4,421 ⟶ 4,683:
<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;">
<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;">
<span style="color: #004080;">atom</span> <span style="color: #000000;">
<span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<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;">
<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;">
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">
<span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<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;">
<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;">
<span style="color: #000000;">
<span style="color: #008080;">if</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
Line 4,454 ⟶ 4,714:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span>
<span style="color: #
<span style="color: #
<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;">
<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;">
<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;">
<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>
<span style="color: #008080;">function</span> <span style="color: #000000;">smartp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">
<span style="color: #008080;">if</span> <span style="color: #000000;">
<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;">
<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>
Line 4,481 ⟶ 4,742:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">
<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;">
<span style="color: #004080;">atom</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">
<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>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
{{out}}
<pre style="font-size: 8px">
Line 4,528 ⟶ 4,794:
=={{header|PHP}}==
<
$smallNumbers = array('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
Line 4,584 ⟶ 4,850:
// The number is zero
return $str . "{$smallNumbers[(int)$thisPart]}";
}</
Example:
<
NumberToEnglish(12);
NumberToEnglish(123);
NumberToEnglish(1234567890123);
NumberToEnglish(65535);
NumberToEnglish(-54321);</
Returns:
<pre>Zero
Line 4,601 ⟶ 4,867:
=={{header|PicoLisp}}==
<
(cond
((=0 N) "zero")
Line 4,622 ⟶ 4,888:
(pack "-" (numNm (% N 10))) ) ) )
((rank N '((100 . "hundred") (1000 . "thousand") (1000000 . "million")))
(pack (numNm (/ N (car @))) " " (cdr @) " " (numNm (% N (car @)))) ) ) )</
=={{header|PL/I}}==
<
('zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
Line 4,678 ⟶ 4,944:
value = value ||h || v || y(i);
end;
put skip edit (trim(N), ' = ', value) (a);</
=={{header|PL/M}}==
Line 4,684 ⟶ 4,950:
works up to 65535.
<
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 4,764 ⟶ 5,030:
CALL SPELL(READ$NUMBER(FCB$NAME));
CALL EXIT;
EOF</
{{out}}
<pre>A>spell 0
Line 4,778 ⟶ 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).
<
IF 0 = number THEN
FUNCTION = "zero"
Line 4,845 ⟶ 5,111:
#ENDIF
? int2Text(n)
END FUNCTION</
Sample output:
Line 4,854 ⟶ 5,120:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Get-NumberName
{
Line 4,986 ⟶ 5,252:
1, 234, 31337, 987654321 | Get-NumberName
</syntaxhighlight>
{{Out}}
<pre>
Line 4,998 ⟶ 5,264:
=={{header|Prolog}}==
<
:- module(spell, [spell/2]).
Line 5,060 ⟶ 5,326:
spell(N, Rest),
string_concat(", ", Rest, S).
</syntaxhighlight>
{{Out}}
<pre>
Line 5,082 ⟶ 5,348:
=={{header|PureBasic}}==
The range of integers handled has been set at an obscene 45 digits.
<
numberNames:
;small
Line 5,202 ⟶ 5,468:
CloseConsole()
EndIf
</syntaxhighlight>
{{out}}
<pre>Give me an integer (or q to quit)! 3
Line 5,216 ⟶ 5,482:
Note: This example is also used as a module in the [[Names to numbers#Python]] task and should be kept in-sync.
<
"fifty", "sixty", "seventy", "eighty", "ninety"]
SMALL = ["zero", "one", "two", "three", "four", "five",
Line 5,279 ⟶ 5,545:
n //= -10
print('%-12i -> %s' % (n, spell_integer(n)))
print('')</
{{out}}
Line 5,309 ⟶ 5,575:
An alternative solution that can name very large numbers.
<
def int_to_english(n):
if n < 0: return "minus " + int_to_english(-n)
Line 5,359 ⟶ 5,625:
print(int_to_english(2 ** 100))
print(int_to_english(10 ** (2*64)))
</syntaxhighlight>
{{out}}
<pre>
Line 5,372 ⟶ 5,638:
{{trans|C#}}
<syntaxhighlight lang="quackery">[ [ table
$ "zero" $ "one" $ "two"
$ "three" $ "four" $ "five"
Line 5,381 ⟶ 5,646:
$ "fourteen" $ "fifteen"
$ "sixteen" $ "seventeen"
$ "eighteen" $ "nineteen" ] do ] is units ( n --> $ )
[ [ table
Line 5,387 ⟶ 5,652:
$ "thirty" $ "fourty" $ "fifty"
$ "sixty" $ "seventy" $ "eighty"
$ "ninety" ] do ] is tens ( n --> $ )
[ $ "" swap
Line 5,404 ⟶ 5,669:
[ 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
[
[ dip [ $ ", " join ] ]
1000000 /mod swap triplet
$ " million" join
swap dip join
Line 5,421 ⟶ 5,707:
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
1+ ** random
dup echo
say " is:"
name$ nest$
60 wrap$ cr cr ]</syntaxhighlight>
{{out}}
<pre>
hundred and ninety one
four
seven hundred, seventy seven billion, one hundred million,
eight hundred, fifty one thousand, two hundred and thirty
six
three
hundred, ninety nine thousand, five hundred and thirteen
million, three hundred, eighty six thousand, three hundred
and seventy
eight
four
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.
<
ones <- c("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
Line 5,526 ⟶ 5,820:
my_num$text <- as.character(lapply(my_num$nums, make_number))
print(my_num, right=F)
</syntaxhighlight>
{{out}}
Line 5,571 ⟶ 5,865:
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 5,609 ⟶ 5,903:
(define r (+ (* e (random e)) (random e)))
(printf "~s: ~a\n" r (integer->english r)))
</syntaxhighlight>
{{out}}
Line 5,631 ⟶ 5,925:
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"
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen>;
constant @X = <0 X twenty thirty forty fifty sixty seventy eighty ninety>;
Line 5,664 ⟶ 5,958:
while '' ne (my $n = prompt("Number: ")) {
say int-name($n);
}</
Output:
<pre>Number: 0
Line 5,681 ⟶ 5,975:
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"
put join "\n", .&cardinal, .&cardinal(:improper) with -7/4;
Line 5,689 ⟶ 5,983:
put join "\n", .&cardinal, .&cardinal-year, .&ordinal, .&ordinal-digit with 1999;
.&cardinal.put for 6.022e23, 42000, π;</
<pre>negative one and three quarters
Line 5,727 ⟶ 6,021:
this simple script available [https://github.com/AbdelrahmanGIT/RingSamples/blob/master/src/ConvertNumbersToString.ring here]
<
OneList=["zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
Line 5,804 ⟶ 6,098:
return str2list(str)
</syntaxhighlight>
{{out}}
for input of:
Line 5,872 ⟶ 6,166:
{{works with|Ruby|1.9.2+}}
<
eleven twelve thirteen fourteen fifteen sixteen seventeen
eighteen nineteen)
Line 5,931 ⟶ 6,225:
puts "Error: #{e}"
end
end</
{{out}}
<pre>
Line 5,955 ⟶ 6,249:
=={{header|Rust}}==
<
const SMALL: &[&str] = &[
Line 6,025 ⟶ 6,319:
write!(&mut stdout, "\n").unwrap();
}
}</
{{out}}
Line 6,040 ⟶ 6,334:
=={{header|Scala}}==
{{libheader|Scala}}
<
import scala.collection.parallel.ParSeq
Line 6,145 ⟶ 6,439:
}.seq.sorted.foreach(num => println(f"$num%+,80d -> ${longhand(numeral = num, showAnd = true)}"))
} // object SpellNumber @ line 110</
{{out}}
Line 6,256 ⟶ 6,550:
=== Recursive ===
Recursive TreeMap solution (for values up to trillions):
<
val NUMBERS = TreeMap(
Line 6,282 ⟶ 6,576:
}
}
</syntaxhighlight>
Examples
Line 6,296 ⟶ 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.
<
include "stdio.s7i";
include "wrinum.s7i";
Line 6,304 ⟶ 6,598:
var integer: number is 0;
begin
for number range 1 to
writeln(str(ENGLISH, number));
end for;
end func;</
=={{header|SenseTalk}}==
<
set numbers to [0,1,7,22,186,pi,-48.6,-3451,925734, 12570902378]
Line 6,318 ⟶ 6,612:
put output
end repeat
</syntaxhighlight>
Output:
<pre>
Line 6,335 ⟶ 6,629:
=={{header|SequenceL}}==
Works on all 32 bit signed integers.<br>
<
import <Utilities/Sequence.sl>;
import <Utilities/Conversion.sl>;
Line 6,395 ⟶ 6,689:
in
hundredsWord ++ andWord ++ tensWord ++ onesWord;</
{{out}}
Line 6,412 ⟶ 6,706:
=={{header|Shale}}==
<
maths library
Line 6,506 ⟶ 6,800:
random maths::() 1000000000 % speak() // Cap it to less than 1 billion.
i++
} while</
{{out}}
Line 6,536 ⟶ 6,830:
=={{header|Sidef}}==
<
say l.num2en(123456789);</
{{out}}
Line 6,545 ⟶ 6,839:
=={{header|SQL}}==
<syntaxhighlight lang="sql">
select val, to_char(to_date(val,'j'),'jsp') name
from
Line 6,556 ⟶ 6,850:
select to_char(to_date(5373485,'j'),'jsp') from dual;
</syntaxhighlight>
<pre>
VAL NAME
Line 6,576 ⟶ 6,870:
{{trans|Kotlin}}
<
private static let bigNames = [
1_000: "thousand",
Line 6,709 ⟶ 7,003:
for number in nums {
print("\(number) => \(number.numberName)")
}</
{{out}}
Line 6,731 ⟶ 7,025:
=={{header|Tcl}}==
<
if { ! [regexp -- {^(-?\d+)$} $n -> n]} {
error "not a decimal integer"
Line 6,802 ⟶ 7,096:
catch {int2words $test} result
puts "$test -> $result"
}</
produces
<div style="width:full;overflow:scroll">
Line 6,827 ⟶ 7,121:
=={{header|VBA}}==
{{trans|Phix}}<
Public decades As Variant
Public orders As Variant
Line 6,940 ⟶ 7,234:
Debug.Print Format(smartp(si), "@@@@@@@@@@@@@@@@"); " "; spell(si)
Next i
End Sub</
<pre> 99 ninety-nine
300 three hundred
Line 6,981 ⟶ 7,275:
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.
<
Private small As Variant, tens As Variant, big As Variant
Line 7,040 ⟶ 7,334:
int2Text$ = Trim$(outP)
End Function</
Example output (in a msgbox) is identical to the BASIC output.
Line 7,051 ⟶ 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.
<
Sub Main()
Line 7,078 ⟶ 7,372:
End Function
End Module</
=={{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.
<
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
Line 7,106 ⟶ 7,468:
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
if (s > 0) t = t + " " + say.call(s)
} else {
Line 7,115 ⟶ 7,476:
n = (n/1000).floor
if (p > 0) {
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
Line 7,127 ⟶ 7,487:
}
for (n in [12, 1048576, 9e18, -2, 0]) System.print(say.call(n))</
{{out}}
Line 7,139 ⟶ 7,499:
=={{header|XPL0}}==
<
proc NumName(Dev, Num); \Output integer Num in prose to device Dev
Line 7,191 ⟶ 7,551:
NumName(0, 1_000_000); CrLf(0);
NumName(0, 1_234_567_890); CrLf(0);
]</
{{out}}
Line 7,205 ⟶ 7,565:
=={{header|zkl}}==
{{trans|Scala}}
<
ns =[1..20].chain([30..90,10]).walk(),
names=T("one","two","three","four","five","six","seven","eight","nine",
Line 7,226 ⟶ 7,586:
}else ""
}(n).strip() // sometimes there is a trailing space
}</
<
println("%,d is %s".fmt(n,numberToString(n)));
}</
{{out}}
<pre>
|