Anonymous user
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}}==
<lang go>package main
Line 1,933:
func main() {
}
}
var small = [...]string{"zero", "one", "two", "three", "four", "five", "six",
var tens = [...]string{"
var illions = [...]string{"", " thousand", " million", " billion",
"
func say(n int64) string {
var t string
switch {▼
t = "negative "
case n < 20:▼
// Note, for math.MinInt64 this leaves n negative.
n = -n
case n < 100:▼
}
t := tens[n/10]▼
s := n % 10▼
if s > 0 {▼
return t▼
s
h := small[n/100] + " hundred"▼
t += "-" + small[s]
}
if s > 0 {▼
case n < 1000:
if s > 0
t += " "
}
for i := 0; n >= 1000; i++ {▼
default:
n /= 1000▼
// work right-to-left
p := n % 1000▼
sx := ""
ix := say(p) + " " + illions[i]▼
if sx != "" {
ix += " " +
}
sx = ix
}
}
t += sx
}
}</lang>
Output:
<pre>
twelve
one million forty
nine quintillion
negative two
zero
</pre>
|