A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
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 58 votes) |
||
If you have the number 28 and you want to display it as 28.00 here's how you can format it:
Of course, if you want to format it to 5 decimal places just use ToString("N5") instead. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentsdo 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 using (N2) returns comma between amount but idont want
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
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
THANK YOU VERY MUCH
Thanks a lot.It works well.After going through lot of sites with big big procedures, dint expect this would be this much easier
Thanks buddy, u made it as easy as to declare a variable.
Thank! It is really useful and save me a lot of time.
Thanks.....it is useful.
if the precision are zeros then this will not
work
thx. i got it.. thx a lot..
Thank you very much, great tip!
Me too! Looked through many sites before stumbling on this simple yet sooo effective method! Thanks a million!
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]
Great!
Gr888888888 man
after searching all world ......
i founound thizzzz amazing methord..
thnx bro...
keeep it up
Thx..this is a life saver
Gawd.. this is so helpful, its very handy for our thesis. thank you so much.. this is something
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.
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
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
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
This rounds off the number. what if the need is to just trim the number to 2 decimal places without rounding off ?
Best! I got what I needed, Helpful one...
Cunt.
wow, you ppl are idiots!
Seriously stupid people!
Thx guys really helpful !!!
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
Thanks guyz, you made my life easier!
thank u v much
Thank you
Hi!
Thanx for provide so easy code. you simply great.
Thanks dude....... its great i just use ToString(".00")and it works for me
i give up on this shit...i cant format numbers to x.
thanks guys,ya'll hav been alot of help.this works out perfect
Thanks its very use full
fdsfsdfdsfs
Thanks Dude
asdsad
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 :)
textBox1.Text = 2935.1941346
string S;
double Number = double.Parse(textBox1.Text);
S = Number.ToString("#.##");
---
S = "2935.19"
thanks
thanks
thanks
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("#.##");
}
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("#.##");
}
Thank you very much..This was more than a useful information...even though too small
Thank you, I feel like shitstorm without this!
My mom fed me lead paint when I was a child.
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
but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated
but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated
but this function rounds of to 2 decimals
i want it to truncate the value to 2 decimals
any idea, greatly appriciated
Thank you!!!!
Thanks, didn't know it was that simple!
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
Hey Guys:
I have decimal number like decimal dec=19999.99999M;
I want dec as 19999.99 with 2 decimal without round anything ...
Thanks
This was helpful.... thanks all
This was helpful.... thanks all
For Decimal number with 476567.00
number.Tostring(".00") that's it
this is great , work like charm
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
Thanks for the help..
Good..!..
Thank you Constantine! I was searching for this desperately.
Thank you Constantine! I was searching for this desperately.
thanks...
U define this in simple way.Thats Good.
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;
its really works..
thanks..
Thanks a lot dear..............
Thanks a lot dear..............
Thanks a lot dear..............
Thanks a lot dear..............
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!
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!
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!
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!
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!
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!
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!
Related Knowledge Base Articles
Related Source Code
Related Tutorials
C# Job SearchFrom 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.