2

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?

TheCarver
  • 19,391
  • 25
  • 99
  • 149

1 Answers1

3

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>"
%>
stealthyninja
  • 10,343
  • 11
  • 51
  • 59