TheCarver
TheCarver

Reputation: 19723

Classic ASP / Round() - Show Numbers 10,000+ as 10K

Like on Stack Overflow's reputation points, if a number is greater than ten thousand, they show it as 10.3K, to save space. Do I achieve this using the Round function or is better to use some kind of string manipulation?

Upvotes: 2

Views: 598

Answers (1)

stealthyninja
stealthyninja

Reputation: 10371

I'd use FormatNumber() to shorten the number when necessary, here's some sample code to demonstrate:

<%
' 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>"
%>

Upvotes: 3

Related Questions