本帖最后由 newswan 于 2021-6-18 19:33 编辑
改进 支持 小数 大写 金额- $dn = [ordered] @{
- "零"=0; "一"=1; "二"=2; "三"=3; "四"=4; "五"=5; "六"=6; "七"=7; "八"=8; "九"=9;
- "〇"=0; "壹"=1; "贰"=2; "叁"=3; "肆"=4; "伍"=5; "陆"=6; "柒"=7; "捌"=8; "玖"=9;
- "拾"="十"; "佰"="百"; "仟"="千"; "两"=2; "廿"="2十";
- }
-
- $dr = [ordered] @{
- "0"=0; "1"=1; "2"=2; "3"=3; "4"=4; "5"=5; "6"=6; "7"=7; "8"=8; "9"=9;
- "十"=10; "百"=100; "千"=1000; "拾"=10; "佰"=100; "仟"=1000;
- "万"=10000;
- "亿"=100000000;
- "兆"=10000000000000000;
- }
-
- $rank = [ordered] @{
- "0"=0; "1"=0; "2"=0; "3"=0; "4"=0; "5"=0; "6"=0; "7"=0; "8"=0; "9"=0;
- "10"=1; "100"=1; "1000"=1;
- "10000"=2;
- "100000000"=3;
- "10000000000000000"=4;
- }
-
- function convert_zwzs ($str)
- {
- #ss [0] 数字 [2~n] 单位
- [long[]] $ss = @(0) * 5
- $i = 0
- $str.GetEnumerator() | foreach {
- $i++
- $v = [long] $dr[$_.ToString()]
- $r = $rank[[string]$v]
- write-Debug ('{0,2} {1,4} {2,4} {3,-8} {4,-6} {5}' -f $i , "b:" , "s:$s" , "v:$v" , "r:$r" , "ss: $ss" )
- if ($r -eq 0)
- {
- $ss[0] = $ss[0] * 10 + $v
- }
- elseif ( $r -eq 1)
- {
- $ss[1] += $ss[0] * $v
- $ss[0] = 0
- }
- elseif ( $r -ge 2)
- {
- for($m = 0 ; $m -le $r - 1 ; $m++)
- {
- $ss[$r] += $ss[$m]
- $ss[$m] = 0
- }
- $ss[$r] *= $v
- }
- write-Debug ('{0,2} {1,4} {2,4} {3,-8} {4,-6} {5}' -f $i , "e:" , "s:$s" , "v:$v" , "r:$r" , "ss: $ss" )
- }
-
- if($str -match "([百千拾佰仟万亿兆]+)(\d+)$") # 末尾数字无单位
- {
- # $m1 = $matches
- $v = 1
- $matches[1].GetEnumerator() | foreach-object {
- $v *= [long] $dr[[string]$_]
- }
- if ($matches[2] -match "^([1-9]*)0*([0-9]*)$")
- {
- $ss[0] = ([int] $matches[1]) * $v / [math]::pow(10,$matches[1].length) + ([int] $matches[2])
- }
- }
-
- $n=$ss.GetUpperBound(0)
- for($m = 0 ; $m -le $n - 1 ; $m++)
- {
- $ss[$n] += $ss[$m]
- $ss[$m] = 0
- }
-
- return $ss[-1]
- }
-
- # 主函数
- function convert_zw_num ($str)
- {
- $str = $str -replace "\s+",""
- if ($str -match "^[-负]")
- {
- $numSgn = "-"
- $str = $str -replace "^[-负]",""
- }
- $dn.GetEnumerator() | ForEach-Object {
- $str = $str -replace $_.key , $_.value
- }
- $str = $str -replace "[元圆正整点.]+","."
- $str = $str -replace "[.]$",""
- $str = $str -replace "^[.]","0."
- $str = $str -replace "(?<=^|[^1-9])(?=[十])","1"
-
- if ($str -match "^[\d.]+$") # 纯数字
- {
- [string] $num = $str
- }
- elseif ($str -match "(\d+[.]\d+)([仟万亿兆]+)$") # 数字 + 单位
- {
- [double] $num1 = $matches[1]
- $matches[2].GetEnumerator() | foreach-object {
- $num1 *= [long] $dr[ [string]$_ ]
- }
- [string] $num = $num1
- }
- elseif ($str -notmatch "([.])") # 中文整数
- {
- [string] $num = convert_zwzs $str
- }
- elseif ($str -match "(.*)[.](\d+)$") # 中文整数.小数
- {
- [string] $num1 = convert_zwzs $matches[1]
- [string] $num2 = $matches[2]
- [string] $num = $num1 + "." + $num2
- }
- elseif ($str -match "[角分]+") # 中文金额
- {
- if ($str -match "([^.角分]*)[.]*(\d+角)*(\d+分)*$")
- {
- [string] $num1 = convert_zwzs $matches[1]
- $num2 = [double] ( $matches[2] -replace "角","" ) /10
- $num2 += [double] ( $matches[3] -replace "分","" ) /100
- [string] $num = [double] $num1 + $num2
- }
- }
-
- return $numSgn + [string] $num
- }
-
- convert_zw_num "壹拾壹元壹角壹分"
复制代码
|