数字大小写、金额、进制转换总结

MsgBox,262144,进制转换测试,% "10—>2:" DigitalConv.ConvertBase(10, 2, "25")
	 . "`n10—>16:" DigitalConv.ConvertBase(10, 16, "44")
	 . "`n10—>8 :" DigitalConv.ConvertBase(10, 8, "44")
	 . "`n16—>10:" DigitalConv.ConvertBase(16, 10, "4E")
	 . "`n16—>8 :" DigitalConv.ConvertBase(16, 8, "4E")
	 . "`n16—>2 :" DigitalConv.ConvertBase(16, 2, "4E")
	 . "`n2 —>10:" DigitalConv.ConvertBase(2, 10, "1011")
	 . "`n2 —>16:" DigitalConv.ConvertBase(2, 16, "1011")
	 . "`n2 —>8 :" DigitalConv.ConvertBase(2, 8, "1011")
	 . "`n8 —>16:" DigitalConv.ConvertBase(8, 16, "35")
Loop
{
	Random, Number, 0.0, 10000000000000.0
	Number:=Format("{:.4f}",Number)
	MsgBox,262149,数字转换测试, % "Number:" Number
		 . "`n数字大写:" DigitalConv.GetDigitalFormat(Number,True)
		 . "`n数字小写:" DigitalConv.GetDigitalFormat(Number,False)
		 . "`n金额转换:" DigitalConv.GetMoneyFormat(Number)
	IfMsgBox,Cancel
		Break
}

Class DigitalConv
{
	static list:={DigitalTrad:{0:"零",1:"壹",2:"贰",3:"叁",4:"肆",5:"伍",6:"陆",7:"柒",8:"捌",9:"玖"}
		,DigitalSimp:{0:"〇",1:"一",2:"二",3:"三",4:"四",5:"五",6:"六",7:"七",8:"八",9:"九"}
		,AmountUnits:["","万","亿","兆","京","垓"]
		,UnitsTrad:["","萬","億","兆","京","垓"]}

	;;大小写转换
	GetDigitalFormat(Num,trad:=False){
		Local String
		String:=""
		if Num~="[^0-9.]"
			Return ""
		if InStr(Num,"."){
			len:=StrLen(Integer:=StrSplit(Num,".")[1]),decimal:=StrSplit(Num,".")[2]
		}Else
			len:=StrLen(Integer:=Num)
		DllCall("msvcrt.dll\_wcsrev", "Ptr", &Integer, "CDecl")
		Integer:=Trim(RegExReplace(Integer,"(\d{4})","$1,"),",")

		Loop,Parse,Integer,`,
		{
			if (A_LoopField<>"") {
				if Item:=this.FormatDigital(A_LoopField,trad?["拾","佰","仟"]:["十","百","千"],True,trad)
					String.= (trad?this.List.UnitsTrad[A_Index]:this.List.AmountUnits[A_Index]) Item
			}
		}
		DllCall("msvcrt.dll\_wcsrev", "Ptr", &String, "CDecl")	;;字符反转
		String:=RegExReplace(RegExReplace(String,(trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]) "{2,}",(trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0])),(trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]) "(?=" (trad?this.List.UnitsTrad[2]:this.List.AmountUnits[2]) "|" (trad?this.List.UnitsTrad[3]:this.List.AmountUnits[3]) "|" (trad?this.List.UnitsTrad[4]:this.List.AmountUnits[4]) "|" (trad?this.List.UnitsTrad[5]:this.List.AmountUnits[5]) "|" (trad?this.List.UnitsTrad[6]:this.List.AmountUnits[6]) ")")
		String:=RegExReplace(String,(trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]) "+$|^" (trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]))

		if decimal {
			String.= trad?"點":"点"
			Loop,Parse,decimal
				String.= trad?this.List.DigitalTrad[A_LoopField]:this.List.DigitalSimp[A_LoopField]
		}

		Return RegExReplace(String,"^" . (trad?this.List.DigitalTrad[1]:this.List.DigitalSimp[1]) . (trad?"拾":"十"),trad?"拾":"十")
	}

	;;金额转换
	GetMoneyFormat(Num){
		Local String
		String:=""
		if Num~="[^0-9.]"
			Return ""
		if InStr(Num,"."){
			len:=StrLen(Integer:=StrSplit(Num,".")[1]),decimal:=StrSplit(Num,".")[2]
		}Else
			len:=StrLen(Integer:=Num)
		DllCall("msvcrt.dll\_wcsrev", "Ptr", &Integer, "CDecl")
		Integer:=Trim(RegExReplace(Integer,"(\d{4})","$1,"),",")

		Loop,Parse,Integer,`,
		{
			if (A_LoopField<>"") {
				if Item:=this.FormatDigital(A_LoopField,["拾","佰","仟"])
					String.= this.List.AmountUnits[A_Index] Item
			}
		}
		DllCall("msvcrt.dll\_wcsrev", "Ptr", &String, "CDecl")	;;字符反转

		String:=RegExReplace(RegExReplace(String,this.List.DigitalTrad[0] "{2,}",this.List.DigitalTrad[0]),this.List.DigitalTrad[0] "(?=" this.List.AmountUnits[2] "|" this.List.AmountUnits[3] "|" this.List.AmountUnits[4] "|" this.List.AmountUnits[5] "|" this.List.AmountUnits[6] ")")
		String:=RegExReplace(String,this.List.DigitalTrad[0] "+$|^" this.List.DigitalTrad[0])
		if decimal {
			if Item:=this.FormatDigital(decimal,["角","分","厘","毫"],False){
				String.= "元" Item
				String:=RegExReplace(String,this.List.DigitalTrad[0] "(角|分|厘|毫)",this.List.DigitalTrad[0])
				String:=RegExReplace(String,this.List.DigitalTrad[0] "+$|^" this.List.DigitalTrad[0])
			}Else
				String.="元整"
		}Else{
			String.="元整"
		}

		Return RegExReplace(String,"^" this.List.DigitalTrad[1] "拾","拾")
	}

	;;千分位匹配
	FormatDigital(Num,Units,Reverse:=True,trad:=True){
		Local String
		String:=""
		if (Num~="^\d+$") {
			Loop,% StrLen(Num)
			{
				if (SubStr(Num,A_Index,1)<>""){
					String.=(trad?this.List.DigitalTrad[SubStr(Num,A_Index,1)]:this.List.DigitalSimp[SubStr(Num,A_Index,1)]) . (!Reverse?SubStr(Num,A_Index,1)?Units[A_Index]:trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]:SubStr(Num,A_Index+1,1)?Units[A_Index]:"")
				}
			}
		}
		String:=RegExReplace(String,(trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]) "{2,}",trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0])

		Return StrLen(Num)-1<objlength(Units)? String . (trad?this.List.DigitalTrad[0]:this.List.DigitalSimp[0]):String
	}
	
	;;2—36进制互转
	ConvertBase(InBase, OutBase, nptr){
		static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
		static v := A_IsUnicode ? "_i64tow"    : "_i64toa"
		VarSetCapacity(s, 66, 0)
		value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InBase, "CDECL Int64")
		DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutBase, "CDECL")
		return s
	}
}

给TA捐赠
共{{data.count}}人
人已捐赠
其他教程

小白学习日记2写gui

2022-1-14 14:13:49

其他

AutoHotkey代码测试助手-2022年1月15日

2022-1-15 10:23:14

4 条回复 A文章作者 M管理员
  1. 蜜獾哥

    应群成员的需求特总结重写了数字转中文格式的方法,可以实现数字转中文、金额、以及各种进制的转换。匆忙之下写的难免有瑕疵如发现有问题的在下面留言!

  2. hexuren

    补充上简介和使用说明岂不是更好?

  3. dbgba
    dbgba给您打赏了¥5
  4. 未名游客给您捐赠了¥10
个人中心
购物车
优惠劵
有新私信 私信列表
搜索