经典ASP / Round() - 显示数字10,000+为10K

时间:2012-02-27 05:43:22

标签: asp-classic vbscript

就像Stack Overflow的声誉点一样,如果一个数字大于一万,它们会显示为10.3K,以节省空间。我是使用Round函数实现这一点还是更好地使用某种字符串操作?

1 个答案:

答案 0 :(得分:3)

我会在必要时使用FormatNumber()来缩短数字,下面是一些示例代码:

<%
' Number scale I used:
' http://www.statman.info/conversions/number_scales.html
Function shorten(s)
    Dim i, f

    i = CDbl(s)

    If (i > 1000000000000) Then
        i = i / 1000000000000
        f = "T"
    ElseIf (i > 1000000000) Then
        i = i / 1000000000
        f = "G"
    ElseIf (i > 1000000) Then
        i = i / 1000000
        f = "M"
    ElseIf (i > 1000) Then
        i = i / 1000
        f = "K"
    End If

    shorten = FormatNumber(i, 2) & f
End Function

Response.Write shorten("1346578977987") & "<br>"
Response.Write shorten("1645112877") & "<br>"
Response.Write shorten("1313333") & "<br>"
Response.Write shorten("108977") & "<br>"
%>