Number names: Difference between revisions

Added Easylang
No edit summary
(Added Easylang)
 
(10 intermediate revisions by 7 users not shown)
Line 862:
=={{header|BASIC}}==
{{works with|QBasic}}
 
<syntaxhighlight lang="qbasic">DECLARE FUNCTION int2Text$ (number AS LONG)
 
Line 936 ⟶ 935:
int2Text$ = RTRIM$(outP)
END FUNCTION</syntaxhighlight>
 
{{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 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>
 
</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}}==
Line 2,022 ⟶ 2,082:
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}}==
Line 2,534 ⟶ 2,752:
 
 
 
=={{header|Fōrmulæ}}==
 
In [https://wiki.formulae.org/Data_types_tutorial#Number_names this] page you can see the solution of this task.
 
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.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
-->
 
=={{header|Go}}==
Line 5,429 ⟶ 5,638:
{{trans|C#}}
 
<syntaxhighlight lang="quackery">[ [ table
[ [ table
$ "zero" $ "one" $ "two"
$ "three" $ "four" $ "five"
Line 5,438 ⟶ 5,646:
$ "fourteen" $ "fifteen"
$ "sixteen" $ "seventeen"
$ "eighteen" $ "nineteen" ] do ] is units ( n --> $ )
 
[ [ table
Line 5,444 ⟶ 5,652:
$ "thirty" $ "fourty" $ "fifty"
$ "sixty" $ "seventy" $ "eighty"
$ "ninety" ] do ] is tens ( n --> $ )
 
[ $ "" swap
Line 5,461 ⟶ 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
[ 1000000over /modsize swap0 triplet> if
[ dip [ $ ", " join ] ]
1000000 /mod swap triplet
$ " million" join
swap dip join
Line 5,478 ⟶ 5,707:
over size 0 > if
[ dip [ $ ", " join ] ]
triplet join ] is name$ ( n --> $ )
dup reverse witheach
[ char , = if
[ i split
behead drop
$ " and" swap
join join
conclude ] ] ] is name$ ( n --> $ )
 
10 times
[ 10 918 random
1+ ** random
dup echo
say " is:"
name$ nest$
60 wrap$ cr cr ]</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
 
<pre>1272741791 is:
onetwo million, seven hundred, andfourty one twentythousand, seven
hundred and ninety one
 
380004 is:
four
thirty eight thousand
 
76818288663798714 is:
seven hundred and sixty eightthree million, oneseven hundred, andninety eight thousand,
eighty two thousand, eightseven hundred and eighty sixfourteen
 
1777100851236 is:
seven hundred, seventy seven billion, one hundred million,
one
eight hundred, fifty one thousand, two hundred and thirty
six
 
36076073689199513 is:
three millionbillion, six hundred, andeighty sevennine thousandmillion, six hundredone
hundred, ninety nine thousand, five hundred and thirteen
and seven
 
5059697703514386370 is:
fiveseven millionhundred, fiftythree nine thousandbillion, sixfive hundred, and ninetyfourteen
million, three hundred, eighty six thousand, three hundred
seven
and seventy
 
96896829121545842 is:
ninetwenty hundred and sixty eightone million, ninefive hundred, andfourty sixtyfive thousand,
eight thousand, two hundred and ninetyfourty onetwo
 
251626133867 is:
twentythree five millionthousand, oneeight hundred and sixty two thousand, sixseven
hundred and thirteen
 
4581964814902020 is:
four hundredmillion, andnine fifty eight millionhundred, onetwo hundredthousand and ninetytwenty
six thousand, four hundred and eighty one
 
47022976290599343 is:
4493774 is:
fourty seven quadrillion, twenty two trillion, nine hundred,
four million, four hundred and ninety three thousand, seven
seventy six billion, two hundred, ninety million, five
hundred and seventy four
hundred, ninety nine thousand, three hundred and fourty
 
three</pre>
 
=={{header|R}}==
Line 6,361 ⟶ 6,598:
var integer: number is 0;
begin
for number range 1 to 9999991000000 do
writeln(str(ENGLISH, number));
end for;
Line 7,137 ⟶ 7,374:
End Module</syntaxhighlight>
 
=={{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"]
Line 7,208 ⟶ 7,445:
{{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="ecmascriptwren">var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
 
Line 7,231 ⟶ 7,468:
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
System.write("") // guards against VM recursion bug
if (s > 0) t = t + " " + say.call(s)
} else {
Line 7,240 ⟶ 7,476:
n = (n/1000).floor
if (p > 0) {
System.write("") // guards against VM recursion bug
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
1,983

edits