标题: [数值计算] 求批处理人民币小写转大写的算法 [打印本页]
作者: winload 时间: 2019-4-12 20:52 标题: 求批处理人民币小写转大写的算法
今天突然想到用批处理做一个人民币小写金额转大写金额的批处理,于是着手去写,谁料写到一半发现思路不对。
主要体现在一个数中连续零的问题没法解决。
于是百度了下别人写的代码,发现也不行。
比如:100000008.00
在此求高手支招!最好奉上代码,有劳!
我原本的思路是:先把金额数字转换为大写,然后补上单位,最后解决关于连续零的问题(无奈发现连续零不好解决)
作者: Batcher 时间: 2019-4-12 21:59
http://bbs.bathome.net/thread-705-1-1.html
http://bbs.bathome.net/thread-13339-1-1.html
这几个代码试过了吗?
作者: winload 时间: 2019-4-13 08:00
回复 2# Batcher
之前在咱们论坛搜索竟然搜不到,多谢了!
作者: lxf0356 时间: 2019-4-15 15:12
回复 2# Batcher
先谢谢管理员,我也正需要这样的批处理。
求修改
1、从粘贴板获取金额,运行后,再输出大写金额到粘贴板。
2、能识别千分符。
谢谢了。- :: 人民币大小写互转程序 R3
- :: qzwqzw http://bbs.cn-dos.net
- :: 2007-01-03 11:15
- :: 出处:http://www.cn-dos.net/forum/viewthread.php?tid=26288
- @echo off
- setlocal EnableDelayedExpansion
-
- for /l %%i in (1,1,100) do (
- set /a numin=!random!*50001-50000000
- set numin=!numin:~0,-2!.!numin:~-2!
- set in=!numin!
- call :Num2RMB
- set in=!out!
- call :RMB2Num
- echo.%%i [!numin!:!in!:!out!]
- if !numin! neq !out! set /p=--- Invalid ---
- set out=
- )
- if "%~0"=="%~f0" pause
- endlocal
- goto :eof
-
- :Num2RMB
- setlocal
- set num=%in%
- set num=0%num%
- for /f "tokens=1,2,* delims=." %%f in ("%num%") do (
- set num2=%%g00
- set num=%%f!num2:~0,2!
- if not "%%h"=="" goto :eof
- )
-
- :del_pre0
- if "%num:~0,1%"=="0" (
- set num=%num:~1%
- goto del_pre0
- )
-
- :checknum
- set num=%num:,=%
- set /a num2=num+0
- if not "%num%"=="%num2%" goto :eof
- if %num% geq 1000000000000 goto :eof
- if %num% leq 0 goto :eof
-
- set tbl1=零壹贰叁肆伍陆柒捌玖
- set tbl2=分角元拾佰仟万拾佰仟亿拾佰仟
-
- :n2r_loop
- call set rmb=%%tbl1:~%num:~-1,1%,1%%%%tbl2:~%bit%,1%%%rmb%
- set /a bit+=1
- set num=%num:~0,-1%
- if not "%num%"=="" goto n2r_loop
-
- set rmb=%rmb:零拾=零%
- set rmb=%rmb:零佰=零%
- set rmb=%rmb:零仟=零%
- set rmb=%rmb:零零=零%
- set rmb=%rmb:零零=零%
-
- set rmb=%rmb:零元=元零%
- set rmb=%rmb:零万=万零%
- set rmb=%rmb:零亿=亿零%
- set rmb=%rmb:零零=零%
-
- set rmb=%rmb:零分=零%
- set rmb=%rmb:零角=零%
- set rmb=%rmb:角零=角%
- set rmb=%rmb:零零=整%
-
- endlocal & set out=%rmb%
- goto :eof
- ::Num2RMB
-
- :RMB2Num
- setlocal
- set rmb=%in%
- set rmb=%rmb:零=%
- set rmb=%rmb:整=%
-
- set tbl1=零壹贰叁肆伍陆柒捌玖
- set tbl2=分角元拾佰仟万拾佰仟亿拾佰仟
- for /l %%i in (1,1,9) do call set rmb%%tbl1:~%%i,1%%=%%i
-
- :r2n_loop
- if "%tbl2:~0,1%"=="%rmb:~-1,1%" (
- set rmb=!rmb:~0,-1!
- call set tmp=%%rmb!rmb:~-1,1!%%
- if not "!tmp!"=="" (
- set num=!tmp!!num!
- set rmb=!rmb:~0,-1!
- ) else (
- set num=0!num!
- )
- ) else (
- set num=0!num!
- )
- set tbl2=%tbl2:~1%
- if not "%rmb%"=="" if not "%tbl2%"=="" goto r2n_loop
-
- set num=%num:~0,-2%.%num:~-2%
- endlocal & set out=%num%
- goto :eof
- ::RMB2Num
复制代码
作者: Batcher 时间: 2019-4-15 16:04
回复 4# lxf0356
clip 命令可以操作剪贴板,你试试看。
作者: smss 时间: 2019-4-18 19:57
本帖最后由 smss 于 2019-4-20 18:17 编辑
上至千极下至小数点后12位 范围0~9999999999999999999999999999999999999999999999999999.999999999999
2019.4.20修复已知BUG- @echo off&setlocal enabledelayedexpansion
- set unit=仟佰拾极仟佰拾载仟佰拾正仟佰拾涧仟佰拾沟仟佰拾穰仟佰拾秭仟佰拾垓仟佰拾京仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元
- set "Z=9999999999999999999999999999999999999999999999999999.999999999999"
- :X
- color 0d
- set /p Z=请输入数字:
- if /i "%Z%"=="q" exit
- if "%Z%"=="0" echo 零元整&goto X
- for /f "tokens=1,2delims=." %%i in ('echo;%Z%^|findstr /b /e "[1-9][0-9]* 0 [1-9][0-9]*\.[0-9]* 0\.[0-9]*"') do (set "a=%%i"&set "b=%%j")
- if defined a (if defined b (if "%b:~12%" NEQ "" (goto U) else if "%a:~52%" NEQ "" (goto U) else (goto Y)
- ) else if "%a:~52%" NEQ "" (goto U) else (goto Y)) else (goto U)
- goto X
- :Y
- set Y=
- for /f "tokens=1,2 delims=." %%a in ("%Z%") do set "Z=%%a"&set "frac=%%b"
- if defined frac (set frac=!frac:~0,12!&set m=!frac:~0,1!&if !m! gtr 0 set Y=!m!角
- set m=!frac:~1,1!&if !m! gtr 0 set Y=!Y!!m!分
- set m=!frac:~2,1!&if !m! gtr 0 set Y=!Y!!m!厘
- set m=!frac:~3,1!&if !m! gtr 0 set Y=!Y!!m!毫
- set m=!frac:~4,1!&if !m! gtr 0 set Y=!Y!!m!丝
- set m=!frac:~5,1!&if !m! gtr 0 set Y=!Y!!m!忽
- set m=!frac:~6,1!&if !m! gtr 0 set Y=!Y!!m!微
- set m=!frac:~7,1!&if !m! gtr 0 set Y=!Y!!m!纤
- set m=!frac:~8,1!&if !m! gtr 0 set Y=!Y!!m!沙
- set m=!frac:~9,1!&if !m! gtr 0 set Y=!Y!!m!尘
- set m=!frac:~10,1!&if !m! gtr 0 set Y=!Y!!m!埃
- set m=!frac:~11,1!&if !m! gtr 0 set Y=!Y!!m!渺)
- set n=0
- for /l %%a in (0,1,100) do if not "!Z:~%%a,1!"=="" set /a n+=1
- if not %Z% equ 0 (for /l %%a in (1,1,%n%) do (set bit=!Z:~-%%a,1!&if not "bit"=="" (set m=!unit:~-%%a,1!&set "Y=!bit!!m!!Y!"
- )))
- set Y=%Y:0=零%
- set Y=%Y:1=壹%
- set Y=%Y:2=贰%
- set Y=%Y:3=叁%
- set Y=%Y:4=肆%
- set Y=%Y:5=伍%
- set Y=%Y:6=陆%
- set Y=%Y:7=柒%
- set Y=%Y:8=捌%
- set Y=%Y:9=玖%
- set Y=%Y:零仟=零%
- set Y=%Y:零佰=零%
- set Y=%Y:零拾=零%
- set Y=%Y:零零零=零%
- set Y=%Y:零零=零%
- set Y=%Y:零极=极%
- set Y=%Y:零载=载%
- set Y=%Y:零正=正%
- set Y=%Y:零涧=涧%
- set Y=%Y:零沟=沟%
- set Y=%Y:零穰=穰%
- set Y=%Y:零秭=秭%
- set Y=%Y:零垓=垓%
- set Y=%Y:零京=京%
- set Y=%Y:零兆=兆%
- set Y=%Y:零亿=亿%
- set Y=%Y:零万=万%
- set Y=%Y:零元=元%
- set Y=%Y:零壹=壹%
- set Y=%Y:零贰=贰%
- set Y=%Y:零叁=叁%
- set Y=%Y:零肆=肆%
- set Y=%Y:零伍=伍%
- set Y=%Y:零柒=柒%
- set Y=%Y:零捌=捌%
- set Y=%Y:零玖=玖%
- set Y=%Y:极载正涧沟穰秭垓京兆亿万=极%
- set Y=%Y:载正涧沟穰秭垓京兆亿万=载%
- set Y=%Y:正涧沟穰秭垓京兆亿万=正%
- set Y=%Y:涧沟穰秭垓京兆亿万=涧%
- set Y=%Y:沟穰秭垓京兆亿万=沟%
- set Y=%Y:穰秭垓京兆亿万=穰%
- set Y=%Y:秭垓京兆亿万=秭%
- set Y=%Y:垓京兆亿万=垓%
- set Y=%Y:京兆亿万=京%
- set Y=%Y:兆亿万=兆%
- set Y=%Y:极载=极%
- set Y=%Y:载正=载%
- set Y=%Y:正涧=正%
- set Y=%Y:涧沟=涧%
- set Y=%Y:正涧=正%
- set Y=%Y:沟穰=沟%
- set Y=%Y:穰秭=穰%
- set Y=%Y:秭垓=秭%
- set Y=%Y:垓京=垓%
- set Y=%Y:京兆=京%
- set Y=%Y:兆亿=兆%
- set Y=%Y:亿万=亿%
- set Y=%Y:穰垓=穰%
- set Y=%Y:载正涧沟穰秭垓京兆亿=%
- set Y=%Y:载正涧沟穰秭垓京兆=%
- set Y=%Y:载正涧沟穰秭垓京=%
- set Y=%Y:载正涧沟穰秭垓=%
- set Y=%Y:载正涧沟穰秭=%
- set Y=%Y:载正涧沟穰=%
- set Y=%Y:载正涧沟穰=%
- set Y=%Y:载正涧沟=%
- set Y=%Y:载正涧=%
- set Y=%Y:正涧沟穰秭垓京兆亿=%
- set Y=%Y:涧沟穰秭垓京兆亿=%
- set Y=%Y:沟穰秭垓京兆亿=%
- set Y=%Y:穰秭垓京兆亿=%
- set Y=%Y:秭垓京兆亿=%
- set Y=%Y:垓京兆亿=%
- set Y=%Y:京兆亿=%
- set Y=%Y:正沟秭京亿=%
- set Y=%Y:正沟秭京=%
- set Y=%Y:正沟秭=%
- set Y=%Y:正沟=%
- color 0b
- if !frac! equ 0 (echo %Y%整) else echo %Y%
- ping 0 -n "2">nul&goto X
- :U
- echo 已超出范围,请重新输入&goto X
复制代码
作者: 523066680 时间: 2019-4-18 21:26
不可说不可说
作者: smss 时间: 2019-4-19 15:30
本帖最后由 smss 于 2019-4-21 11:42 编辑
迷你版- @echo off&setlocal EnableDelayedExpansion
- set a=零壹贰叁肆伍陆柒捌玖
- set b=分角元拾佰仟万拾佰仟亿拾佰仟
- set "Z=999999999999.99"
- :X
- color 0d
- setlocal
- ::对输入数字处理,取出整数和小数
- set /p Z=请输入金额:
- if /i "%Z%"=="q" exit
- if "%Z%"=="0" echo 零元整&goto X
- for /f "tokens=1,2 delims=." %%a in ('echo;%Z%^|findstr /b /e "[1-9][0-9]* 0 [1-9][0-9]*\.[0-9]* 0\.[0-9]*"') do (set Z2=%%b00&set Z=%%a!Z2:~0,2!&set "i=%%a"&set "j=%%b")
- if defined i (if defined j (if "%j:~2%" NEQ "" (goto U) else if "%i:~12%" NEQ "" (goto U) else (goto Y)
- ) else if "%i:~12%" NEQ "" (goto U) else (goto Y)) else (goto U)
- :Y
- call set Y=%%a:~%Z:~-1,1%,1%%%%b:~%bit%,1%%%Y%
- set /a bit+=1
- set Z=%Z:~0,-1%
- if not "%Z%"=="" goto Y
- set Y=%Y:零壹=壹%
- set Y=%Y:零贰=贰%
- set Y=%Y:零叁=叁%
- set Y=%Y:零肆=肆%
- set Y=%Y:零伍=伍%
- set Y=%Y:零柒=柒%
- set Y=%Y:零捌=捌%
- set Y=%Y:零玖=玖%
- set Y=%Y:零仟=零%
- set Y=%Y:零佰=零%
- set Y=%Y:零拾=零%
- set Y=%Y:零零零=零%
- set Y=%Y:零零=零%
- set Y=%Y:零万=万元%
- set Y=%Y:零亿=亿元%
- set Y=%Y:零零=零%
- set Y=%Y:零分=零%
- set Y=%Y:零角=零%
- set Y=%Y:角零=角%
- set Y=%Y:零零=%
- set Y=%Y:零亿=亿%
- set Y=%Y:零万=万%
- set Y=%Y:零元=%
- set Y=%Y:零万=元%
- set Y=%Y:亿万=亿%
- color 0b
- if !j! equ 0 (echo %Y%整) else echo %Y%
- endlocal
- ping 0 -n "2">nul&goto X
- :U
- echo 已超出范围,请重新输入&goto X
复制代码
作者: lxf0356 时间: 2019-4-21 00:05
回复 8# smss
求修改
1、从粘贴板获取金额,运行后,再输出大写金额到粘贴板。
2、能识别千分符。
作者: smss 时间: 2019-4-21 10:59
回复 smss
求修改
1、从粘贴板获取金额,运行后,再输出大写金额到粘贴板。
2、能识别千分符。 ...
lxf0356 发表于 2019-4-21 00:05
http://bbs.bathome.net/thread-8329-1-1.html
作者: lxf0356 时间: 2019-4-23 14:27
本帖最后由 lxf0356 于 2019-4-29 15:12 编辑
迷你版
smss 发表于 2019-4-19 15:30
借用你的代码,修改了一下,搞定了
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |