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?
Asked
Active
Viewed 565 times
2
-
Sounds like a little bit of division is in order. – Michael Petrotta Feb 27 '12 at 05:47
-
2@AnthonyWJones: is that what Stack Overflow is for; so amateurs can post questions that can be mocked by the experienced? It's not very encouraging... – TheCarver Feb 27 '12 at 20:45
1 Answers
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
-
+1 for the technique although your math is a little off where Mega is concerned. – AnthonyWJones Feb 27 '12 at 07:45
-
@AnthonyWJones - Oops, you're quite right. Fixed it now and added giga as well. – stealthyninja Feb 27 '12 at 09:45