469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

fomat string with 2 decimal places

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.

Jul 10 '07 #1
2 9105
<so******@yahoo.comwrote:
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().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 10 '07 #2

<so******@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
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.

Jul 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brent Bortnick | last post: by
1 post views Thread by Gizmo | last post: by
1 post views Thread by MAF | last post: by
3 posts views Thread by David | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.