Click to See Complete Forum and Search --> : Inserting commas in numbers
Crash1hd
August 11th, 2005, 01:59 PM
Hello everyone,
I am trying to figure out how to add commas to my text?
for example if I have the code
this.physicalmemory.Text = (y/1024) + " KB";
and the output say is 2096620 KB
I am trying to get the output to say 2,096,620 KB
thanks in advance :)
zips
August 11th, 2005, 02:34 PM
Use this.physicalmemory.Text.ToString("N0"). (That's a zero, not letter oh)
Crash1hd
August 11th, 2005, 02:37 PM
I tried that I am now getting the following error
The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
Argument '1': cannot convert from 'string' to 'System.IFormatProvider'
zips
August 11th, 2005, 02:50 PM
Sorry. Let me try again:
int aInt = Convert.ToInt32(y / 1024.0);
string aStr = aInt.ToString("N0") + " KB";
The 0 after the decimal point is required.
Crash1hd
August 11th, 2005, 03:55 PM
Thankyou for the response works great however I had to alter it a little when I tried to set it the way you had it int aInt = Convert.ToInt32(y / 1024.0); it told me Operator '/' cannot be applied to operands of type 'string' and 'double'
so I used
int y = Convert.ToInt32(x);
int z = (y / 1024);
string aStr = z.ToString("N0") + " KB";
this.physicalmemory.Text = aStr;
and it works great so anyone that is trying to get the total physical memory of windows to show up as text see below
ManagementObjectSearcher query1 = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem") ;
ManagementObjectCollection queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
string x = mo["totalphysicalmemory"].ToString();
int y = Convert.ToInt32(x);
int z = (y / 1024);
string aStr = z.ToString("N0") + " KB";
this.physicalmemory.Text = aStr;
}
and make sure to add System.Management to the references :) or you will get an error!
zips
August 12th, 2005, 10:42 AM
Operator '/' cannot be applied to operands of type 'string' and 'double'
So, that means your original plan included dividing a text string by a float. Hmmm, I don't think I've ever seen that attempted before!
Anyway, thanks for the tip on memory size.
torrud
August 12th, 2005, 11:28 AM
You can also simple use:
int number = 2000000;
string test =number.ToString("#,#");
So you will get seperators dependent on your current culture info settings. :wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.