Geekpedia Programming Tutorials






How can I format a number to x decimal places?

On Friday, July 23rd 2004 at 12:00 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.6 with 11 votes)
Advertisement
More C# Resources
If you have the number 28 and you want to display it as 28.00 here's how you can format it:

int val = 28;
string formatted = val.ToString("N2");

Of course, if you want to format it to 5 decimal places just use ToString("N5") instead.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by lami on Friday, October 14th 2005 at 09:02 AM

do you need to specific includes (e.g. system IO) to get this result... I keep getting below error when i try this

[no overload for method 'tostring' takes one argument]

by Anjum Rizwi on Tuesday, May 9th 2006 at 03:52 AM

by using (N2) returns comma between amount but idont want

by Constantine on Wednesday, May 17th 2006 at 09:24 AM

Dude,
use this.

double number = 5650.343;

Console.WriteLine("The Number Is {0}", number.ToString("#0.00"));

The # can be extended to number of characters.

If you want a digit to formatted into 0000.00 and then again return as integer.

double number = 123456.646;
number = double.Parse(number.ToString("####0.00"));

Adios

by Vijay on Saturday, June 10th 2006 at 02:28 AM

Its a really good example of convert.... i have tries many things to format number to 2 decimal function in C#.. but dont know that it was so simple

by SANTHOSH THOMAS on Sunday, June 11th 2006 at 07:50 AM

THANK YOU VERY MUCH

by S.Amirthalakshmi on Wednesday, June 21st 2006 at 10:55 AM

Thanks a lot.It works well.After going through lot of sites with big big procedures, dint expect this would be this much easier

by Sumit Ghose on Friday, June 23rd 2006 at 07:27 AM

Thanks buddy, u made it as easy as to declare a variable.

by Chris on Sunday, January 14th 2007 at 05:54 PM

Thank! It is really useful and save me a lot of time.

by Rajendra on Monday, February 12th 2007 at 01:50 AM

Thanks.....it is useful.

by trupti on Thursday, February 22nd 2007 at 04:24 AM

if the precision are zeros then this will not
work

by melee on Thursday, April 26th 2007 at 01:27 AM

thx. i got it.. thx a lot..

by Frano on Monday, May 7th 2007 at 02:59 PM

Thank you very much, great tip!

by twopoke on Thursday, May 31st 2007 at 03:23 AM

Me too! Looked through many sites before stumbling on this simple yet sooo effective method! Thanks a million!

by Chip on Friday, July 27th 2007 at 06:34 AM

I get the same as Imani...

do you need to specific includes (e.g. system IO) to get this result... I keep getting below error when i try this

[no overload for method 'tostring' takes one argument]

by Martin on Friday, October 26th 2007 at 10:49 AM

Great!

by Adonis on Monday, November 26th 2007 at 02:13 PM

Gr888888888 man

after searching all world ......

i founound thizzzz amazing methord..

thnx bro...

keeep it up

by Dee on Wednesday, January 9th 2008 at 03:19 PM

Thx..this is a life saver

by ruel on Friday, February 8th 2008 at 10:56 AM

Gawd.. this is so helpful, its very handy for our thesis. thank you so much.. this is something

by Goldfinger on Tuesday, February 26th 2008 at 10:51 AM

Just as a side note, if you need to round off to x number of decimal places while preserving the data type (double, float, or whaterver), you can also use the Math.Round(xxx, yy) function, which takes some numeric type xxx and rounds it to yy decimal places, and preserves the type. This may be useful if you need to round but still need to use the rounded number in other math operations.

by Huzefa on Saturday, March 8th 2008 at 05:18 AM

hey guys,
I have a decimal whole number and i want that number with 2 decimal point in a decimal variable ie
decimal i =799;
i = decimal.Parse(i.ToString("#0.00"));

this thing is not working for me...the number after conversion is still 799 and NOT 799.00...
any other ideas guys as to how to go about....
thanx

by Deems on Monday, March 10th 2008 at 02:56 AM

Huzefa,

To format your decimal do the following instead:

decimal i = 799M;

When you wish to write it out with a specific format usse:

string blah = i.ToString(#0.00);

You cannot set the number of decimal points of a numeric data type - only when converting it to a string as above.

HTH

by Mitch on Thursday, March 13th 2008 at 07:37 PM

If your dealing with money you can format it like this to output $1000.00

string sString = mDecimal.ToString("C");

this worked for decimal probably the same for other types

by Gaurav on Friday, September 12th 2008 at 05:32 AM

This rounds off the number. what if the need is to just trim the number to 2 decimal places without rounding off ?

by Mahesh on Thursday, October 23rd 2008 at 01:34 AM

Best! I got what I needed, Helpful one...

by we on Thursday, October 23rd 2008 at 09:17 AM

Cunt.

by Anon on Monday, November 3rd 2008 at 04:01 AM

wow, you ppl are idiots!

by sam on Friday, December 26th 2008 at 02:31 PM

Seriously stupid people!

by CR4CkH34D on Wednesday, February 4th 2009 at 03:04 PM

Thx guys really helpful !!!

by No one on Wednesday, March 11th 2009 at 03:29 PM

Thanks for tip.
you can do:
string formatted = val.ToString("N" num);
when num is the number of digits you want, so
you can set it as you like

by Sechaba on Monday, March 23rd 2009 at 04:33 AM

Thanks guyz, you made my life easier!

by rohan on Saturday, June 13th 2009 at 04:34 AM

thank u v much

by Sri on Monday, June 29th 2009 at 11:39 PM

Thank you


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons