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
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
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.
 

Vanell2K

New member
Joined
Feb 16, 2006
Messages
2
Programming Experience
5-10
# 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
 

bmilano

Member
Joined
Nov 15, 2006
Messages
10
Programming Experience
10+
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.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
yes, store all the information in the same manor throughout the application, it will help other who may need to modify it later
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
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:

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+

cjelec

New member
Joined
Jan 7, 2007
Messages
3
Programming Experience
5-10
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... :)
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
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.
 
Top Bottom