By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

easy way to format numbers to strings?

P: 93
In C#, I'd like to convert some numbers to strings in a standard format, but preferably in a simple way, such as if you were going to output them rather than store them in string variables, a la:
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine ("{0:F2}", num);
rather than use a bunch or string methods to make them all the same number of significant digits. Isn't there any way to do this?
Mar 2 '09 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 7,872
Well there is the .ToString() function on every data type
Mar 2 '09 #2

Expert 2.5K+
P: 3,525
Expanding on what Plater said... .ToString() has some overrides that allow you to use nearly all the same format commands as what you included in your question.

Expand|Select|Wrap|Line Numbers
  1. float bob = 69.486;
  2. string strBob = bob.ToString("{0:F2}");
Should work. Might need a little massaging; its been a while since I've done that.
Mar 3 '09 #3

Expert 5K+
P: 7,872
I think its just .ToString("F2");
Mar 3 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.