fomat string with 2 decimal places

S

soni2926

hi,
i have the following being returned to be in a string:
20.5
i need to make this with 2 places after the decimal, like 20.50,
anyway to do that? I've tried the following but it doesn't seem to be
working:
string newstring = string.Format("{0:#.##}", price.ToString());

price is of type Object.

Thanks.
 
J

Jon Skeet [C# MVP]

i have the following being returned to be in a string:
20.5
i need to make this with 2 places after the decimal, like 20.50,
anyway to do that? I've tried the following but it doesn't seem to be
working:
string newstring = string.Format("{0:#.##}", price.ToString());

price is of type Object.

You're trying to format a string as if it were a number. Try just
removing the call to ToString().
 
M

Mr. Arnold

hi,
i have the following being returned to be in a string:
20.5
i need to make this with 2 places after the decimal, like 20.50,
anyway to do that? I've tried the following but it doesn't seem to be
working:
string newstring = string.Format("{0:#.##}", price.ToString());

price is of type Object.

The ToString() can apply formatting itself.

price.ToString("0.00") and may other forms of formatting currency, numeric
data with decimals, dates etc, ect.

Use Google and look it up if you like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top