Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
58
Task/Number-names/Go/number-names.go
Normal file
58
Task/Number-names/Go/number-names.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
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{"", "", "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
|
||||
if n < 0 {
|
||||
t = "negative "
|
||||
// Note, for math.MinInt64 this leaves n negative.
|
||||
n = -n
|
||||
}
|
||||
switch {
|
||||
case n < 20:
|
||||
t += small[n]
|
||||
case n < 100:
|
||||
t += tens[n/10]
|
||||
s := n % 10
|
||||
if s > 0 {
|
||||
t += "-" + small[s]
|
||||
}
|
||||
case n < 1000:
|
||||
t += small[n/100] + " hundred"
|
||||
s := n % 100
|
||||
if s > 0 {
|
||||
t += " " + say(s)
|
||||
}
|
||||
default:
|
||||
// work right-to-left
|
||||
sx := ""
|
||||
for i := 0; n > 0; i++ {
|
||||
p := n % 1000
|
||||
n /= 1000
|
||||
if p > 0 {
|
||||
ix := say(p) + illions[i]
|
||||
if sx != "" {
|
||||
ix += " " + sx
|
||||
}
|
||||
sx = ix
|
||||
}
|
||||
}
|
||||
t += sx
|
||||
}
|
||||
return t
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue