【问题标题】:How to put comma after 3 digits in numeric variable in vbscript?如何在 vbscript 中的数字变量中的 3 位数字后面加上逗号?
【发布时间】:2020-01-31 18:24:37
【问题描述】:

我想在 vbscript 中的数字变量中的 3 位数字后面加一个逗号

w_orimpo = getvalue(rsmodifica , "w_orimpo")
w_orimpo = FormatNumber(w_orimpo,2)

w_orimpo的初始值为21960。

如果我使用 FormatNumber,我得到的值是 21,960。

但我想得到以下一个 -> 219,60

【问题讨论】:

  • 您的语言环境中, 是小数分隔符还是千位分隔符?当您执行FormatNumber(w_orimpo, 0) 时,您会得到什么?

标签: vbscript numbers format


【解决方案1】:

我们可以通过正则表达式替换来处理这个问题:

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, "$1,")
output = regex2.Replace(output, "")
Rhino.Print output

请注意,这里需要两个正则表达式替换,因为 VBScript 的正则表达式引擎不支持环视。有一个单一的正则表达式模式可以在这里完成工作:

(\d{3})(?!$)

这将一次仅匹配(并捕获)三个数字的组,并且仅当这三个数字没有跟在输入的末尾时。这需要涵盖以下边缘情况:

123456 -> 123,456

我们不希望在最后一组三位数后面加逗号。我的回答通过另一个正则表达式替换来解决这个问题,以修剪任何尾随逗号。

【讨论】:

    【解决方案2】:

    或者没有正则表达式:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2020-11-18
      • 2023-02-21
      • 2021-03-04
      • 2015-03-09
      相关资源
      最近更新 更多