943,640 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1397
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 2nd, 2009
0

easy way to format numbers to strings?

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
BobLewiston is offline Offline
105 posts
since Nov 2008
Mar 2nd, 2009
3

Re: easy way to format numbers to strings?

String.Format?

C# Syntax (Toggle Plain Text)
  1. string s = String.Format("{0:F2}", num);
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 2nd, 2009
0

Re: easy way to format numbers to strings?

Great. Exactly what I was looking for. Thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster
BobLewiston is offline Offline
105 posts
since Nov 2008
Mar 2nd, 2009
0

Re: easy way to format numbers to strings?

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.
Last edited by ddanbe; Mar 2nd, 2009 at 2:19 pm.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,736 posts
since Oct 2008
Mar 2nd, 2009
1

Re: easy way to format numbers to strings?

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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);
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 2nd, 2009
0

Re: easy way to format numbers to strings?

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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,736 posts
since Oct 2008
Mar 2nd, 2009
-1

Re: easy way to format numbers to strings?

C# Syntax (Toggle Plain Text)
  1. string s = int.parse(numberValue);

Works great.
I too was very confused when I first saw members to an int datatype...
Last edited by skatamatic; Mar 2nd, 2009 at 3:35 pm.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Mar 2nd, 2009
0

Re: easy way to format numbers to strings?

¿que?
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,736 posts
since Oct 2008
Mar 3rd, 2009
0

Re: easy way to format numbers to strings?

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Mar 3rd, 2009
0

Re: easy way to format numbers to strings?

Click to Expand / Collapse  Quote originally posted by LizR ...
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!).
Last edited by skatamatic; Mar 3rd, 2009 at 4:12 pm.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: DataGridView: Populate Combobox for each row.
Next Thread in C# Forum Timeline: vrooom vroooom... help me speed up my app...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC