12,943,742 members (62,863 online)
Rate this:
See more:
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:

## 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```
Rate this:

## 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
v2
Rate this:

## Solution 1

Hi,

You can use below:

```decimal temp = (decimal)(6900.00 * (5.00 / 100.00));
Response.Write(string.Format("{0:###0.00}", temp));```
karthik mahalingam01 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.

Top Experts
Last 24hrsThis month
 Maciej Los 230 CHill60 170 OriginalGriff 143 ppolymorphe 140 RyanDev 85
 OriginalGriff 4,643 CHill60 2,970 Maciej Los 2,348 Jochen Arndt 1,900 ppolymorphe 1,765