Click here to Skip to main content
12,943,742 members (62,863 online)
Rate this:
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
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:
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:
Rate this: bad
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:[^].

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:[^],[^],[^].

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2017
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