标题: [原创代码] powershell 中文数字转阿拉伯数字 [打印本页]
作者: newswan 时间: 2021-6-15 12:58 标题: powershell 中文数字转阿拉伯数字
本帖最后由 newswan 于 2021-6-18 19:35 编辑
中文数字转阿拉伯数字,只支持整数,可以继续添加 小数 负数 大写- function hz_num ()
- {
- $a = [ordered] @{"零"=0; "一"=1; "二"=2; "三"=3; "四"=4; "五"=5; "六"=6; "七"=7; "八"=8; "九"=9}
- $b = [ordered] @{"十"=10; "百"=100; "千"=1000}
- $c = [ordered] @{"万"=10000}
- $d = [ordered] @{"亿"=100000000}
-
- $str = $args[0] -replace "(?<=^|[^一二三四五六七八九])十","一十"
- $x = $str -split ""
-
- [long[]] $ss = @(0) * 3
- $i = 0
- while ($i -le $x.count)
- {
- $i++
- $s = $x[$i]
- if ($a.keys -contains $s)
- {
- $ss[0] = $ss[0] * 10 + $a[$s]
- } elseif ($b.keys -contains $s)
- {
- $ss[1] += $ss[0] * $b[$s]
- $ss[0] = 0
- } elseif ($c.keys -contains $s)
- {
- $ss[1] = ($ss[1] + $ss[0]) * $c[$s]
- $ss[0] = 0
- } elseif ($d.keys -contains $s)
- {
- $ss[2] = ($ss[2] + $ss[1] + $ss[0]) * $d[$s]
- $ss[1] = 0
- $ss[0] = 0
- } elseif ($s -eq "")
- {
- $ss[2] = $ss[2] + $ss[1] + $ss[0]
- $ss[1] = 0
- $ss[0] = 0
- }
- }
-
- return $ss[2]
- }
-
- hz_num "一万一千零一亿零二十万三百三十三"
复制代码
作者: newswan 时间: 2021-6-17 13:03
本帖最后由 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 "壹拾壹元壹角壹分"
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |