如何在 vbscript 中的数字变量的 3 位数字后加上逗号?
How to put comma after 3 digits in numeric variable in vbscript?
我想在 vbscript 中的数字变量的 3 位数字后加一个逗号
w_orimpo = getvalue(rsmodifica , "w_orimpo")
w_orimpo = FormatNumber(w_orimpo,2)
w_orimpo的初始值为21960。
如果我使用 FormatNumber,我得到值 21,960。
但我想得到下面的 -> 219,60
我们可以通过正则表达式替换来处理这个问题:
Dim input, output, regex1, regex2
Set input = "21960"
Set regex1 = New RegExp
Set regex2 = New RegExp
regex1.Pattern = "(\d{3})"
regex2.Pattern = ",$"
output = regex1.Replace(input, ",")
output = regex2.Replace(output, "")
Rhino.Print output
请注意,此处需要两个正则表达式替换,因为 VBScript 的正则表达式引擎不支持环视。有一个正则表达式模式可以在这里完成工作:
(\d{3})(?!$)
这将一次只匹配(并捕获)三位数字组,并且前提是这三位数字后面没有跟在输入的末尾。这需要涵盖以下边缘情况:
123456 -> 123,456
最后一组三位数后不需要逗号。我的答案通过对 trim 任何尾随逗号进行另一个正则表达式替换来解决这个问题。
或没有正则表达式:
Mid(CStr(w_orimpo), 1, 3) & "," & Mid(CStr(w_orimpo), 4)
或者
Dim divider
divider = 10 ^ (Len(CStr(w_orimpo)) - 3)
w_orimpo = FormatNumber(w_orimpo / divider, 2)
我想在 vbscript 中的数字变量的 3 位数字后加一个逗号
w_orimpo = getvalue(rsmodifica , "w_orimpo")
w_orimpo = FormatNumber(w_orimpo,2)
w_orimpo的初始值为21960。
如果我使用 FormatNumber,我得到值 21,960。
但我想得到下面的 -> 219,60
我们可以通过正则表达式替换来处理这个问题:
Dim input, output, regex1, regex2
Set input = "21960"
Set regex1 = New RegExp
Set regex2 = New RegExp
regex1.Pattern = "(\d{3})"
regex2.Pattern = ",$"
output = regex1.Replace(input, ",")
output = regex2.Replace(output, "")
Rhino.Print output
请注意,此处需要两个正则表达式替换,因为 VBScript 的正则表达式引擎不支持环视。有一个正则表达式模式可以在这里完成工作:
(\d{3})(?!$)
这将一次只匹配(并捕获)三位数字组,并且前提是这三位数字后面没有跟在输入的末尾。这需要涵盖以下边缘情况:
123456 -> 123,456
最后一组三位数后不需要逗号。我的答案通过对 trim 任何尾随逗号进行另一个正则表达式替换来解决这个问题。
或没有正则表达式:
Mid(CStr(w_orimpo), 1, 3) & "," & Mid(CStr(w_orimpo), 4)
或者
Dim divider
divider = 10 ^ (Len(CStr(w_orimpo)) - 3)
w_orimpo = FormatNumber(w_orimpo / divider, 2)