easy way to format numbers to strings?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 80
Reputation: BobLewiston is an unknown quantity at this point 
Solved Threads: 0
BobLewiston BobLewiston is offline Offline
Junior Poster in Training

easy way to format numbers to strings?

 
0
  #1
Mar 2nd, 2009
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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,046
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: easy way to format numbers to strings?

 
3
  #2
Mar 2nd, 2009
String.Format?

  1. string s = String.Format("{0:F2}", num);
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 80
Reputation: BobLewiston is an unknown quantity at this point 
Solved Threads: 0
BobLewiston BobLewiston is offline Offline
Junior Poster in Training

Re: easy way to format numbers to strings?

 
0
  #3
Mar 2nd, 2009
Great. Exactly what I was looking for. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,941
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 279
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: easy way to format numbers to strings?

 
0
  #4
Mar 2nd, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,046
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: easy way to format numbers to strings?

 
1
  #5
Mar 2nd, 2009
Originally Posted by ddanbe View Post
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);
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,941
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 279
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: easy way to format numbers to strings?

 
0
  #6
Mar 2nd, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: easy way to format numbers to strings?

 
-1
  #7
Mar 2nd, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,941
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 279
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: easy way to format numbers to strings?

 
0
  #8
Mar 2nd, 2009
¿que?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: easy way to format numbers to strings?

 
0
  #9
Mar 3rd, 2009
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.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: easy way to format numbers to strings?

 
0
  #10
Mar 3rd, 2009
Originally Posted by LizR View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC