Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Am calculating small values in decimal, Expected result is = 345.00 but Result getting is = 345.0000
 
my code is this
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
Math.Round(tempdiscountprice, 2);
also but it wont work here. Help me
Posted 27-Nov-13 20:19pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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:
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:
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:
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:
66.66666:66.67
66.67:66.67
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
You can use below:
 
decimal temp = (decimal)(6900.00 * (5.00 / 100.00));
Response.Write(string.Format("{0:###0.00}", temp));
  Permalink  
Comments
karthik mahalingam01 at 28-Nov-13 4:24am
   
this code block is working fine..
 
@srihari, you can use this code..
srihari1904 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 359
1 _Amy 230
2 OriginalGriff 200
3 Manfred R. Bihy 200
4 Peter Leow 180
0 OriginalGriff 7,495
1 Sergey Alexandrovich Kryukov 6,402
2 Maciej Los 3,849
3 Peter Leow 3,568
4 CHill60 2,702


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100