Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Am calculating small values in decimal, Expected result is = 345.00 but Result getting is = 345.0000

my code is this
C#
decimal temp= 6900 * ( 5 / 100);


temp = 345.0000 how to round this ? I want to show 345.00 ? whats my mistake here ? I tried this code
C#
Math.Round(tempdiscountprice, 2);
also but it wont work here. Help me
Posted

Numeric values do not have a "number of digits" which they remember. Only when you actually output them as strings to the user or a file can you specific exactly how they are displayed.
There are a number of things wrong with your code:
C#
decimal temp= 6900 * ( 5 / 100);
Is not "345.0000": it is 0.0 because 5 is an integer value, as is 100, and 5 / 100 is zero in integer math. So what you actually need to do is:
C#
decimal temp= 6900M * ( 5M / 100M);
to indicate Decimal numebers and Decimal math.
In this case, Math.Round will do nothing, as the value of 345 is already rounded to 2 decimal places: Rounding removed trailing digits and converts numbers like 66.66666 to 66.67.

To format a number to a specific number of decimal places, you need to convert it to a string, and specify the number of digits you want:
C#
decimal temp = 6900M * (5M / 100M);
decimal rounded = Math.Round(temp, 2);
Console.WriteLine("{0:0.00}:{1:0.00}", temp, rounded);
Which will give you:
345.00:345.00
345.00:345.00
Or:
C#
decimal temp = 66.66666M;
decimal rounded = Math.Round(temp, 2);
Console.WriteLine("{0}:{1}", temp, rounded);
Console.WriteLine("{0:0.00}:{1:0.00}", temp, rounded);
Which gives you:
VB
66.66666:66.67
66.67:66.67
 
Share this answer
 
Most likely, you don't really want to round a numeric value. Rounding per se is pretty rarely used and can even be dangerous, because rounding of some intermediate values in calculations can cause precision loss. Most likely, you only need to show a number on screen in rounded form. This is something a bit different: you can use string formatting instead. Please see all the methods ToString here: http://msdn.microsoft.com/en-us/library/system.decimal%28v=vs.110%29.aspx[^].

As you can see, the format of the numeric value can be done differently, depending on culture (current thread culture or explicitly specified via the IFormatProvider parameter), options and/or format string. For format strings, please see:
http://msdn.microsoft.com/en-us/library/fzeeb5cd%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v2
Hi,

You can use below:

C#
decimal temp = (decimal)(6900.00 * (5.00 / 100.00));
Response.Write(string.Format("{0:###0.00}", temp));
 
Share this answer
 
Comments
Karthik_Mahalingam 28-Nov-13 4:24am    
this code block is working fine..

@srihari, you can use this code..
srihari1904 28-Nov-13 4:38am    
Thankyou Guys, I got now, I changed to temp.ToString("N2") while storing. Sorry small modifucation both values are decimal that is 6900.00 & 5.00. Now I sloved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900