# GO 大写金额转换

x_focus · · 3364 次点击 · · 开始浏览

--------------------------------------------------------------------------------------------------------------------------------------

```package main

import (
"fmt"
"strconv"
_ "strings"
"math"
"regexp"
)
func AmountConvert(p_money float64,p_round bool)string{
var   NumberUpper =[]string{"壹", "贰","叁","肆","伍","陆","柒","捌","玖","零"}
var   Unit =[]string{"分","角","圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"}
var   regex=[][]string{
{"零拾","零"},{"零佰","零"},{"零仟","零"},{"零零零","零"},	{"零零","零"},
{"零角零分","整"},{"零分","整"},{"零角","零"},{"零亿零万零元","亿元"},
{"亿零万零元","亿元"},{"零亿零万","亿"},{"零万零元","万元"},{"万零元","万元"},
{"零亿","亿"},{"零万","万"},{"拾零圆","拾元"},{"零圆","元"},{"零零","零"}}
str,DigitUpper,Unit_Len,round:="","",0,0
if (p_money==0){return "零"	}
if (p_money<0){str="负";p_money=math.Abs(p_money)}
if (p_round){
round=2
}else{
round=1}
Digit_byte:=[]byte(strconv.FormatFloat(p_money,'f',round+1,64)) //注意币种四舍五入
Unit_Len=len(Digit_byte)-round

for _,v:=range(Digit_byte){
if (Unit_Len>=1 && v!=46){
s,_:=strconv.ParseInt(string(v), 10, 0)
if (s!=0){
DigitUpper=NumberUpper[s-1]

}else{
DigitUpper="零"
}
str=str+DigitUpper+Unit[Unit_Len-1]
Unit_Len=Unit_Len-1
}
}
for i,_:=range(regex){
reg:= regexp.MustCompile(regex[i][0])
str=reg.ReplaceAllString(str,regex[i][1])
}
if (string(str[0:3])=="元"){
str= string(str[3:len(str)])
}
if (string(str[0:3])=="零"){
str= string(str[3:len(str)])
}
return str
}

func main(){
fmt.Println(AmountConvert(700.098,true))
//test example
//-1010000011.21 负壹拾亿壹仟万零壹拾壹圆贰角壹分
//1010000011.21  壹拾亿壹仟万零壹拾壹圆贰角壹分
//1000010000.01  壹拾亿零壹万元零壹分
//700.13 柒佰元壹角叁分
//700.03 柒佰元零叁分
//700.098 柒佰元壹角整
//700.98 柒佰元玖角捌分
//70.98 柒拾元玖角捌分
//7.98  柒圆玖角捌分
//0.098 壹角整
//0.98  玖角捌分
//98   玖拾捌圆整
//
}

```

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

--------------------------------------------------------------------------------------------------------------------------------------

```package main

import (
"fmt"
"strconv"
_ "strings"
"math"
"regexp"
)
func AmountConvert(p_money float64,p_round bool)string{
var   NumberUpper =[]string{"壹", "贰","叁","肆","伍","陆","柒","捌","玖","零"}
var   Unit =[]string{"分","角","圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"}
var   regex=[][]string{
{"零拾","零"},{"零佰","零"},{"零仟","零"},{"零零零","零"},	{"零零","零"},
{"零角零分","整"},{"零分","整"},{"零角","零"},{"零亿零万零元","亿元"},
{"亿零万零元","亿元"},{"零亿零万","亿"},{"零万零元","万元"},{"万零元","万元"},
{"零亿","亿"},{"零万","万"},{"拾零圆","拾元"},{"零圆","元"},{"零零","零"}}
str,DigitUpper,Unit_Len,round:="","",0,0
if (p_money==0){return "零"	}
if (p_money<0){str="负";p_money=math.Abs(p_money)}
if (p_round){
round=2
}else{
round=1}
Digit_byte:=[]byte(strconv.FormatFloat(p_money,'f',round+1,64)) //注意币种四舍五入
Unit_Len=len(Digit_byte)-round

for _,v:=range(Digit_byte){
if (Unit_Len>=1 && v!=46){
s,_:=strconv.ParseInt(string(v), 10, 0)
if (s!=0){
DigitUpper=NumberUpper[s-1]

}else{
DigitUpper="零"
}
str=str+DigitUpper+Unit[Unit_Len-1]
Unit_Len=Unit_Len-1
}
}
for i,_:=range(regex){
reg:= regexp.MustCompile(regex[i][0])
str=reg.ReplaceAllString(str,regex[i][1])
}
if (string(str[0:3])=="元"){
str= string(str[3:len(str)])
}
if (string(str[0:3])=="零"){
str= string(str[3:len(str)])
}
return str
}

func main(){
fmt.Println(AmountConvert(700.098,true))
//test example
//-1010000011.21 负壹拾亿壹仟万零壹拾壹圆贰角壹分
//1010000011.21  壹拾亿壹仟万零壹拾壹圆贰角壹分
//1000010000.01  壹拾亿零壹万元零壹分
//700.13 柒佰元壹角叁分
//700.03 柒佰元零叁分
//700.098 柒佰元壹角整
//700.98 柒佰元玖角捌分
//70.98 柒拾元玖角捌分
//7.98  柒圆玖角捌分
//0.098 壹角整
//0.98  玖角捌分
//98   玖拾捌圆整
//
}

```