Last active
April 26, 2018 11:24
-
-
Save amorist/a32edf9238f0a865bf1bf67a0b07c372 to your computer and use it in GitHub Desktop.
ConvertNumToCn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func ConvertNumToCn(num int) string { | |
strnum := strconv.Itoa(num) | |
chineseDigits := []string{"", "一", "二", "三", "四", "五", "六", "七", "八", "九"} | |
sliceUnit := []string{"万", "千", "百", "十", ""} | |
s := sliceUnit[len(sliceUnit)-len(strnum) : len(sliceUnit)] | |
var cn = "" | |
switch strnum { | |
case "0": | |
cn = "开板" | |
break | |
case "1": | |
cn = "首板" | |
break | |
default: | |
if len(strnum) == 1 { | |
cn = chineseDigits[num] | |
} else if len(strnum) == 2 && string(strnum[0]) == "1" { | |
val, _ := strconv.Atoi(string(strnum[1])) | |
cn = "十" + chineseDigits[val] | |
} else { | |
for k, v := range strnum { | |
val, _ := strconv.Atoi(string(v)) | |
cn += chineseDigits[val] + s[k] | |
} | |
} | |
} | |
return cn | |
} | |
func main() { | |
fmt.Println(ConvertNumToCn(1453)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment