用单位符号“k”格式化数字和“m”

发布于 2024-10-18 13:56:58 字数 235 浏览 2 评论 0原文

在 VBScript 中,我正在寻找一个函数,它将返回格式为 1000 或 1000,000 或更大的数字,如下所示:

x = 100,000 then return 100k
y = 500,000 then return 500k
z = 5,000,000 then return 5m
q = 25,000,000 then return 25m 

其中 x、y、z、q 是整数。

In VBScript I'm looking for a function that will return the numbers in the format 1000s or if 1000,000 or greater in millions as follows:

x = 100,000 then return 100k
y = 500,000 then return 500k
z = 5,000,000 then return 5m
q = 25,000,000 then return 25m 

Where x, y, z, q are integers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-10-25 13:56:58

@van:工作示例——

Option Explicit

Function NumFormat(ByRef iNumber, ByRef blnFixed)
    Dim sNumber

    If iNumber >= 1000000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000000) & "m"
        Else
            sNumber = Fix(iNumber / 1000000) & "m"
        End If
    ElseIf iNumber >= 100000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    ElseIf iNumber >= 10000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    ElseIf iNumber >= 1000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    End If

    NumFormat = sNumber
End Function

Response.Write NumFormat(56120000, True) & "<br>"
Response.Write NumFormat(25050000, False) & "<br>"
Response.Write NumFormat(255000, False) & "<br>"
Response.Write NumFormat(9009, True) & "<br>"
Response.Write NumFormat(3000, False) & "<br>"

@van: Working example --

Option Explicit

Function NumFormat(ByRef iNumber, ByRef blnFixed)
    Dim sNumber

    If iNumber >= 1000000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000000) & "m"
        Else
            sNumber = Fix(iNumber / 1000000) & "m"
        End If
    ElseIf iNumber >= 100000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    ElseIf iNumber >= 10000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    ElseIf iNumber >= 1000 Then
        If NOT blnFixed Then
            sNumber = (iNumber / 1000) & "k"
        Else
            sNumber = Fix(iNumber / 1000) & "k"
        End If
    End If

    NumFormat = sNumber
End Function

Response.Write NumFormat(56120000, True) & "<br>"
Response.Write NumFormat(25050000, False) & "<br>"
Response.Write NumFormat(255000, False) & "<br>"
Response.Write NumFormat(9009, True) & "<br>"
Response.Write NumFormat(3000, False) & "<br>"
给妤﹃绝世温柔 2024-10-25 13:56:58
Option Explicit

Public Function convertAmount(a)
    a = CStr(a)
    Dim re : Set re = new RegExp

    ' Replace millions
    re.Pattern ="(,\d{3}){2}$"
    a = re.replace(a, "m")
    ' Replace kilo's
    re.Pattern =",\d{3}$"
    a = re.replace(a, "k")

    convertAmount = a
End Function

' Testcode
MsgBox convertAmount("1,234,567")

它可能不适用于整数,因为我从未见过具有千位分隔符格式的整数。只需在正则表达式中的逗号后面放置一个问号,它也适用于没有千位分隔符的数字。喜欢:
"(,?\d{3}){2}$"",?\d{3}$"

限制:不进行舍入,仅对结果进行聚合

Option Explicit

Public Function convertAmount(a)
    a = CStr(a)
    Dim re : Set re = new RegExp

    ' Replace millions
    re.Pattern ="(,\d{3}){2}$"
    a = re.replace(a, "m")
    ' Replace kilo's
    re.Pattern =",\d{3}$"
    a = re.replace(a, "k")

    convertAmount = a
End Function

' Testcode
MsgBox convertAmount("1,234,567")

It could be it wouldn't work for integers, because I never have seen integers with thousand separator formatting. Just place a question mark in the regular expressions behind the comma, and it will also work for numbers without thousand separators. Like:
"(,?\d{3}){2}$" and ",?\d{3}$"

Limitations: No rounding, only trunking of the results

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文