Number names: Difference between revisions

→‎{{header|Go}}: support zero and negatives, use hyphens (makes it easier to re-use for Four is magic and Four is the... tasks)
m (→‎{{header|REXX}}: changed whitespace in the REXX section header.)
(→‎{{header|Go}}: support zero and negatives, use hyphens (makes it easier to re-use for Four is magic and Four is the... tasks))
Line 1,927:
 
=={{header|Go}}==
PositiveSupports integers, from <code>math.MinInt64 + 1</code> to <code>math.MaxInt64</code>.
<lang go>package main
 
Line 1,933:
 
func main() {
for _, n := range []int64{12, 1048576, 9e18, -2, 0} {
fmt.Println(say(n))
}
}
}
 
var small = [...]string{"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tens = [...]string{"ones", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"}
var illions = [...]string{"", " thousand", " million", " billion",
" "trillion", " quadrillion", " quintillion"}
 
func say(n int64) string {
var t string
switch {
case if n < 1:0 {
t = "negative "
case n < 20:
// Note, for math.MinInt64 this leaves n negative.
return small[n]
n = -n
case n < 100:
}
t := tens[n/10]
switch {
s := n % 10
case n < 20:
if s > 0 {
t += " " + small[sn]
case n < 100:
}
t :+= tens[n/10]
return t
s case:= n <% 1000:10
if s > 0 {
h := small[n/100] + " hundred"
t += "-" + small[s]
s := n % 100
}
if s > 0 {
case n < 1000:
h += " " + say(s)
t h :+= small[n/100] + " hundred"
}
s := n % 10100
return h
if s > 0 default:{
t += " " sx :=+ say(n % 1000s)
}
for i := 0; n >= 1000; i++ {
default:
n /= 1000
// work right-to-left
p := n % 1000
sx := ""
if p > 0 {
for i := 0; n >= 10000; i++ {
ix := say(p) + " " + illions[i]
p := n % 1000
if sx > "" {
n /= 1000
ix += " " + sx
if sp > 0 {
}
ix := say(p) + " " + illions[i]
sx = ix
if sx != "" {
}
ix += " " + }sx
}
return sx
sx = ix
}
}
return ""
}
t += sx
}
return t
}</lang>
Output:
<pre>
twelve
one million forty -eight thousand five hundred seventy -six
nine quintillion
negative two
zero
</pre>
 
Anonymous user