Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.2 with 59 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

by sachin on Tuesday, July 7th 2009 at 03:40 AM

Hi!
Thanx for provide so easy code. you simply great.

by Avinash on Monday, July 13th 2009 at 12:13 AM

Thanks dude....... its great i just use ToString(".00")and it works for me

by jerry on Tuesday, August 4th 2009 at 03:40 PM

i give up on this shit...i cant format numbers to x.

by Sechaba on Wednesday, August 5th 2009 at 01:57 AM

thanks guys,ya'll hav been alot of help.this works out perfect

by Anu on Thursday, August 20th 2009 at 06:34 AM

Thanks its very use full

by cvxvxcvcx on Wednesday, August 26th 2009 at 07:49 AM

fdsfsdfdsfs

by Basha on Monday, November 9th 2009 at 09:19 AM

Thanks Dude

by cxvcvvxcvxcv on Tuesday, November 10th 2009 at 07:19 AM

asdsad

by jan on Tuesday, November 10th 2009 at 09:18 AM

wow, u guys are searching the web and don't find any help beside of this entry? never heard of msdn? string.format()? that would help.. ;)

but anyway, found what i was looking for ("N", used in string.format), since i always forget about how all these formating-letters are working. and this page loads faster than msdn. hooray :)

by Rydel on Sunday, November 15th 2009 at 01:06 AM

textBox1.Text = 2935.1941346

string S;
double Number = double.Parse(textBox1.Text);
S = Number.ToString("#.##");

---

S = "2935.19"

by Deepak MIttal on Tuesday, November 17th 2009 at 11:42 PM

thanks

by Deepak MIttal on Tuesday, November 17th 2009 at 11:42 PM

thanks

by Deepak MIttal on Tuesday, November 17th 2009 at 11:43 PM

thanks

by Mike on Tuesday, December 22nd 2009 at 09:36 AM

Have to check if its valid.

textBox1.Text = "2935.1941346";
string S = string.Empty;

double buffNumber = 0;
if (double.TryParse(textBox1.Text, out buffNumber)
{
S = Number.ToString("#.##");
}

by Mike on Tuesday, December 22nd 2009 at 09:36 AM

Have to check if its valid.

textBox1.Text = "2935.1941346";
string S = string.Empty;

double buffNumber = 0;
if (double.TryParse(textBox1.Text, out buffNumber)
{
S = Number.ToString("#.##");
}

by Robss on Friday, January 8th 2010 at 05:14 AM

Thank you very much..This was more than a useful information...even though too small

by boofoo on Monday, March 29th 2010 at 07:50 PM

Thank you, I feel like shitstorm without this!

by ateLeadPaint on Monday, March 29th 2010 at 08:05 PM

My mom fed me lead paint when I was a child.

by pinky on Thursday, April 1st 2010 at 02:27 AM

hi ! i am using money datatype for all monetary columns in sqlserver2005 database. i want to parse it in asp.net whats its compitable datatype in asp.net

by san on Tuesday, May 25th 2010 at 04:16 AM

but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated

by san on Tuesday, May 25th 2010 at 04:17 AM

but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated

by san on Tuesday, May 25th 2010 at 04:17 AM

but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated

by channa on Tuesday, September 7th 2010 at 02:46 PM

Thank you!!!!

by jonathan on Wednesday, September 22nd 2010 at 04:38 AM

Thanks, didn't know it was that simple!

by Santa Satan on Sunday, October 3rd 2010 at 05:58 PM

It keep rounding my numbers when I use this method which DOESNT work for my. I am making a program to calculate the check digit for a trailer vin number. How do you trim a double or decimal ex. I have a number 29.8181818181818181 when I use this method it gives me 29.82!! I want 29.81. math.floor() or something? floorboard

by Vinod on Monday, January 31st 2011 at 12:29 PM

Hey Guys:

I have decimal number like decimal dec=19999.99999M;
I want dec as 19999.99 with 2 decimal without round anything ...

Thanks

by suji on Sunday, February 6th 2011 at 04:50 AM

This was helpful.... thanks all

by suji on Sunday, February 6th 2011 at 04:52 AM

This was helpful.... thanks all

by .Net on Wednesday, April 20th 2011 at 12:46 AM

For Decimal number with 476567.00

number.Tostring(".00") that's it
this is great , work like charm

by Pierre on Friday, May 27th 2011 at 09:28 AM

Hi,

I've the following problem: I've some decimal numbers that can have 3-4 or even more number after the .


Like this:
4.9 or 4.95 or 4.96547

with this: number.ToString(".##"); I've maximum 2 decimal displayed.
How can I do to get a floating decimal number displayed ?

Thanks

by Ravindra on Monday, June 20th 2011 at 05:35 AM

Thanks for the help..

by George on Monday, June 20th 2011 at 06:38 AM

Good..!..

by shrinivas on Tuesday, July 19th 2011 at 07:16 AM

Thank you Constantine! I was searching for this desperately.

by shrinivas on Tuesday, July 19th 2011 at 07:16 AM

Thank you Constantine! I was searching for this desperately.

by Yusuf Çağrı on Thursday, November 24th 2011 at 04:44 PM

thanks...

by Ray on Friday, February 10th 2012 at 06:12 AM

U define this in simple way.Thats Good.

by Praveen on Thursday, May 10th 2012 at 06:37 AM

Decimal d1=2.3447656;
Decimal d2=2.3443656;

string result1,result2;
result1=Math.Round(d1,3,MidpointRounding.AwayFromZero)
result2=Math.Round(d2,3,MidpointRounding.AwayFromZero)

//Display result as
result1=2.345;
result1=2.344;

by Seju on Monday, June 4th 2012 at 06:48 AM

its really works..
thanks..

by Mahesh on Friday, August 31st 2012 at 04:51 AM

Thanks a lot dear..............

by Mahesh on Friday, August 31st 2012 at 04:51 AM

Thanks a lot dear..............

by Mahesh on Friday, August 31st 2012 at 04:51 AM

Thanks a lot dear..............

by Mahesh on Friday, August 31st 2012 at 04:51 AM

Thanks a lot dear..............

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:40 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:40 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:43 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:43 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:43 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by LeAngolanCoder on Sunday, September 23rd 2012 at 09:43 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!

by Vasco on Sunday, September 23rd 2012 at 09:45 AM

Hello programmer, I wish I could give Y'all my credit card numbers and PIN to thank you so so much because you've just helped me a million.

I've struggling with this way of representing an double with 2 decimal places for 3 years but today I found the answer men.

special thanks to:
No one on Wednesday, March 11th 2009 at 03:29 PM

ToString("N") is the answer I needed. Peace!


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 >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources