Convert 1000 to 1K

Vanell2K

New member
Joined
Feb 16, 2006
Messages
2
Programming Experience
5-10
are there any functions in Dot Net that will Convert thousands into a short format.

For example, I want to display 20,000 as 20K
 
Is it a number? Is it in a textbox? Is it a field in a dataset? What format is it in? Does it have the comma?

You could do a replace on a string.

VB.NET:
         Try
            TextBox3.Text = TextBox3.Text.Replace(",000", "K")
        Catch ex As Exception

        End Try
or with an integer as a string:

VB.NET:
         number1 = 25000
        Try
            Dim string1 As String = number1.ToString
            TextBox3.Text = string1.Replace("000", "K")
        Catch ex As Exception

        End Try

There are probably lots of ways to do it.
 
# format

Thanks for the reply David,

I realize there are a number of ways to do this through a custom function, I was just wondering if there was an inherit way to do it already. ie) the format function
 
Not that you asked, but...

One suggestion I would have is that you always store the actual value in your program rather than the display value. i.e. store "2K" as 2000 instead of 2.

The reason I bring this up is that many years ago I was working on an old COBOL system, and it was totally inconsistent on how it stored numbers. Half the percentages in the system stored 100% as 100, and half stored it as 1.0.
 
yes, store all the information in the same manor throughout the application, it will help other who may need to modify it later
 
If you are using K I would guess you are rounding to the nearest thousanths. So this function is perfect.

VB.NET:
Public Function ConvertToFromK(ByVal Value as Integer) as String
    If Value < 1000 then Return Value.ToString
    Return Value/1000 & "k"
End Function
 
Public Function ConvertToFromK(ByVal Value as String) as Integer
    If Not Value.toLower.EndsWith(k) then Return Cint(Value)
    Return Value*1000
End Function

This is an overloaded function so normally if you put in 2k it automatically responds with 2000 and if you put 2000 it responds 2k.
 
Last edited:
I created the following code that converts lengths of files into kb,mb,gb values:
VB.NET:
Public Function DataLength(ByVal Length As Integer, Optional ByVal Precision As Integer = 3) As String
        Dim tmp As Double = Length
        Dim ex As String = "B"

        If tmp > 1024 Then
            tmp = tmp / 1024
            ex = "KB"
            If tmp > 1024 Then
                tmp = tmp / 1024
                ex = "MB"
                If tmp > 1024 Then
                    tmp = tmp / 1024
                    ex = "GB"
                    If tmp > 1024 Then
                        tmp = tmp / 1024
                        ex = "TB"
                    End If
                End If
            End If
        End If

        Return Math.Round(tmp, Precision) & " " & ex
    End Function
If you are after normal (non-byte) values then change the '1024' to '1000' and remove the 'B'. This will only handle numeric values, if you wanted to use '1,000' then remove ',' from the string first:

VB.NET:
dim ReturnAns As string = DataLength(Val(MyText.Remove(",")),2)
Hope this helps... :)
 
That's a cool function and very handy. But we were just looking for a way to convert '1000' to '1k' and so on. If the request was for more I would have updated my response as well. You should post this function in a new thread maybe and title it 'Usefull Functions' or something and possbily start a trend. Shoot, that sounds like a good idea, if you don't I might, lol.
 
Back
Top