| | |
easy way to format numbers to strings?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 0
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: 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?
C# Syntax (Toggle Plain Text)
Console.WriteLine ("{0:F2}", num);
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);
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
C# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
![]() |
Similar Threads
- high digit numbers (C++)
- C++ Books (C++)
- Open In New Window Php (PHP)
- I lack focus... (Java)
Other Threads in the C# Forum
- Previous Thread: DataGridView: Populate Combobox for each row.
- Next Thread: vrooom vroooom... help me speed up my app...
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart basic bitmap box broadcast buttons c# check checkbox client combobox contorl control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment development disabled displayingopenforms draganddrop drawing editing editor encryption enum event excel file form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox mandelbrot math mathematics mouseclick mysql operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox rows server setup sleep socket sql statistics stream string table text textbox thread time timer update user usercontrol validation visualstudio webbrowser windows winforms wpf xml






