954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

easy way to format numbers to strings?

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:

Console.WriteLine ("{0:F2}", num);


rather than use a bunch of string methods to make them all the same number of significant digits. Isn't there any way to do this?

BobLewiston
Junior Poster
105 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

String.Format?

string s = String.Format("{0:F2}", num);
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Great. Exactly what I was looking for. Thanks.

BobLewiston
Junior Poster
105 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

You can even do things like
s += String.Format("{0:F2} ", num1);
s += String.Format("{0:F3}", num2);
This would give you a string of two numbers separated by a space.
One with 2 digits after the decimal point(which is the default, you don't have to mention it if you like) and one with 3 digits after the decimal point.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
s += String.Format("{0:F2} ", num1); s += String.Format("{0:F3}", num2);

Ruh roh, you forgot to use code tags!

But seriously in that contrived case you'd just write string s = String.Format("{0:F2} {1:F3}", num1, num2);

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Rashakil : You are absolutly right! But...
This was a Quick Reply to Thread!
And I rarely use code tags for one or two lines of code, if I hurted your feelings : I'm sorry.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
string s = int.parse(numberValue);


Works great.
I too was very confused when I first saw members to an int datatype...

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

¿que?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

With everything in .net being a class/object, what I think he means is if you were in c++ or any other lanugage, a data type such as int/string/char etc has no methods, its a datatype, whereas in .net, of course it has methods.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 
With everything in .net being a class/object, what I think he means is if you were in c++ or any other lanugage, a data type such as int/string/char etc has no methods, its a datatype, whereas in .net, of course it has methods.

Haha yeah that's what I meant. I guess not everyone started in the realm of c++. If you did, you would have great respect for many of the features in c#, but also great dissapointment in the speed of execution (there's no pointers!).

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 
you would have great respect for many of the features in c#, but also great dissapointment in the speed of execution (there's no pointers!).


Today it is the speed of development that counts.
If you want to use pointers in C# go ahead!
Why do you think there is a C in C#?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You