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.3 with 25 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
Related Knowledge Base Articles
Related Source Code
Related Tutorials
C# Job Search