So ive googled, to no avail.
How can I put a "," every 3 numbers in an ever changing number?
as in
20000000 = 20,000,000
in VB
thanks.
-
-
Have a look for integer.tostring in the docs.
-
Did you try the Format() method? Something like:
s = Format(i, "###.###")
(Not sure if it must be a . or a , because it might depend on your locale) -
Microsoft.VisualBasic.Strings.FormatNumber() is the method call you're looking for.
The example at the bottom shows how to use it to group digits with commas:
Dim TestNumber As Integer = 45600
' Returns "45,600.00".
Dim TestString As String = FormatNumber(TestNumber, 2, , , TriState.True)
-
Another way to do things (that is not VB specific, but works on all .Net languages):
Dim number As Integer = 12345678
Dim numberString As String = number.ToString("0,0")
The result will be "12,345,678".
The presence of a comma anywhere in the number format string (doesn't matter where, as long as it's to the left of the decimal point (if there is any)) will output the proper thousand separator for the locale. You always use a comma in the format string, regardless of your own locale (the format strings are locale independant).
So the above would print "12.345.678" on my PC because I use a Dutch locale.
You can further control the output by specifying an IFormatProvider.
For instance:
' Always returns "12,345,678" regardless of the current PC's locale
number.ToString("0,0", System.Globalization.CultureInfo.InvariantCulture)
' Always returns "1,23,45,678", as per the Indian format:
number.ToString("0,0", System.Globalization.CultureInfo.GetCultureInfo("mr-in"))
Also check this MSDN page. -
I second the use of a locale-aware method.
-
Here's an awesome cheat sheet for .NET formatting strings.
http://john-sheehan.com/blog/index.php/net-format-string-cheat-sheet/ -
Thanks all.
Not for me though, a mate thats too lazy to sign up.
Sound familiar Jez
-
Apparently, I need it in VB 6.0
Sorry for any inconvenience... -
So, you tried Format() ?

-
Don't ask me, I'm just a messanger boy.
Also, yes he tried it, apparently it didn't format the number at all. -
Hmm, now it's getting complicated, so many solutions but...

-
I bet he is doing something like;
dim i as long
i =20000000
dim j as long
j = format(i, "0,0")
whereas what he should be doing is
dim i as long
i =20000000
dim s as string
s = format(i, "0,0")
If he turns it back to a number it will lose the formating information.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.