473,487 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

easy way to format numbers to strings?

93 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine ("{0:F2}", num);
rather than use a bunch or string methods to make them all the same number of significant digits. Isn't there any way to do this?
Mar 2 '09 #1
3 1128
Plater
7,872 Recognized Expert Expert
Well there is the .ToString() function on every data type
Mar 2 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
Expanding on what Plater said... .ToString() has some overrides that allow you to use nearly all the same format commands as what you included in your question.

Expand|Select|Wrap|Line Numbers
  1. float bob = 69.486;
  2. string strBob = bob.ToString("{0:F2}");
  3.  
Should work. Might need a little massaging; its been a while since I've done that.
Mar 3 '09 #3
Plater
7,872 Recognized Expert Expert
I think its just .ToString("F2");
Mar 3 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2762
by: Rusty Shackleford | last post by:
I have a Summer in front of me without any school, and I'd like to add a new format for python print strings that will show any number in a binary representation. For example: >>> '%b' % 3 11...
15
42976
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
6
6110
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
7
12581
by: Sobhan | last post by:
Hi all, Iam writing a small C program to print a array of numbers in Comma Separated Values in VC++ editor. Lets say: Title Title1 Title2 ----- ------ ------ 10,20,30 50,90,100 120,180,300
11
5918
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
4
6501
by: Jan | last post by:
Is there a way to display numbers in specified format in data grid. I know that I can do this converting numbers to strings but in the grid I want them as numbers (because of sorting). For...
3
2463
by: Gary Wessle | last post by:
Hi I wrote 2 functions "included at the bottom" one inserts and one reads data from a file, the data format is as here **************** a 2 b -0.0336974 _time 1.17312e+09 ****************
7
13607
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.