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?

String.Format?

string s = String.Format("{0:F2}", num);

Great. Exactly what I was looking for. Thanks.

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.

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 : 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.

string s = int.parse(numberValue);

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

commented: fail! -2

¿que?

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.

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!).

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#?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.