How to round to a specific decimal accuracy? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
I have a value var i = 0.69999980926513672. I need to round this value to 0.7 is there a built-in method that will do this?

Use one of:
System.Math.Round (i, 1, MidpointRounding.ToEven);
System.Math.Round (i, 1, MidpointRounding.AwayFromZero);
The difference is how it handles numbers that are equidistant to the rounding point (e.g., 0.65 in your case could either go to 0.7 or 0.6).
Here is a answer I gave to another question which holds much more information.

You are looking for the Math.Round method.
//first param is number to round
//second param is the accuracy to use in the rounding (number of decimal places)
Math.Round(i, 2)

Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));
-- edit
wow, you blink and there are 5 other answers!

http://msdn.microsoft.com/en-us/library/system.math.round%28VS.71%29.aspx

Use the Math.Round method:
double i = 0.69999980926513672;
double result = Math.Round(i, 2);

Related

Why does the decimal 2.5M round to 2? [duplicate]

This question already has answers here:
Why does Math.Round(2.5) return 2 instead of 3?
(15 answers)
Closed 6 years ago.
I must be missing some subtlety of .NET rounding. So I am looking at this example:
decimal num = 2.5M;
var result = Math.Round(num);
Why is result = 2? (I would have expected 3 since it should round up)
See MSDN:
Decimal Math.Round(Decimald)
Rounds a decimal value to the nearest integer, and rounds midpoint
values to the nearest even number (example).
"Midpoint" here means .5; the even number in your case is 2. If you rounded 3.5 this way, it would result in 4.
If you want to use "away from zero" rounding instead, you can use the System.MidpointRounding.AwayFromZero enum:
decimal d = 2.5M;
decimal roundedD = Math.Round(d, MidpointRounding.AwayFromZero); // results in 3
Regarding why it uses midpoint rounding (AKA "banker's rounding") by default instead of "away from zero" rounding, see this answer. Supposedly it's a better algorithm (i.e. more efficient over many iterations).
If you want classic rounding use
decimal num = 2.5M;
var result = Math.Round(num,0, MidpointRounding.AwayFromZero);
Please see https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx for details

c# calculation only returns natural number instead of decimal [duplicate]

When I make a division in C#, it automaticaly rounds down. See this example:
double i;
i = 200 / 3;
Messagebox.Show(i.ToString());
This shows me a messagebox containing "66". 200 / 3 is actually 66.66666~ however.
Is there a way I can avoid this rounding down and keep a number like 66.6666667?
i = 200 / 3 is performing integer division.
Try either:
i = (double)200 / 3
or
i = 200.0 / 3
or
i = 200d / 3
Declaring one of the constants as a double will cause the double division operator to be used.
200/3 is integer division, resulting in an integer.
try 200.0/3.0
200 / 3 this is an integer division. Change to: 200.0 / 3 to make it a floating point division.
You can specify format string with the desired number of decimal ponits:
double i;
i = 200 / 3.0;
Messagebox.Show(i.ToString("F6"));
Though the answer is actually 66.666, what is happening is that 200 / 3 is being calculated resulting in an integer. The integer is then being placed in the float. The math itself is happening as integer math. To make it a float, use 200.0 / 3. The .0 will cause it to treat 200 as a float, resulting in floating point math.
Aside from the double vs int happening in that action, you're thinking of double as a precise unit. Try using the decimal datatype when you really care about accuracy.
More information at this answer:
decimal vs double! - Which one should I use and when?
double i = 200.0 / 3;
double i = ((double)200)/3;
What happens is the two integers perform an integer divide, and then the integer answer is assigned to the float. To avoid that, always cast one of the numbers as a double.
Try this
i = 200d/3d;
and it will not round.
200 and 3 are both integers, so the result will be an integer. Convert one of them to a decimal.
All given answers are wrong because they translate the integer division into one of kind double, which is cleanly not what was asked for (at least from a performance standpoint). The obvious answer is elementary school math, multiply by 10, add 5 and divide again, all integer.
i = (2000 / 3 + 5 ) / 10
You are catching a second division here, which is better than doing double conversions but still far from perfect. You could go even further and multiply by another factor and add other values than five, thus allowing you to use right shifting instead of dividing by 10. The exact formula for doing this is left as an exercise to the reader. (Just google "divisions with Multiply Shift")
Have a nice day.

Math.Round() call in .NET using negative precision [duplicate]

This question already has answers here:
Built in .Net algorithm to round value to the nearest 10 interval
(8 answers)
Closed 9 years ago.
I have a requirement to perform rounding of values with negative precision. As far as I've checked, Math.Round() in .NET does not support negative precision. For example:
ROUND(43.34566,-1)
The above returns 40.
Please suggest how to achive this.
Move the comma to the precision you want to give back and then round.
double n = 43.34566;
double roundingValue = -1;
double precision = Math.Pow(10, roundingValue);
n *= precision;
double result = Math.Round(n, 0) / precision;
ROUND(43.34566 / 10,0) * 10 will work; generalise as appropriate.
Quite simple really:
Math.Round(num / Math.Pow(10.0, -(precision))) * Math.Pow(10.0, -(precision));
//this won't work for positives but oh well

Problem with Double and division [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Ints and Doubles doing division
1/252 = 0 in c#?
Hi,
Maybe because it's Friday, but I cannot understand this:
(Double)1/2 = 0.5
(Double)1/(Double)2 = 0.5
(Double)((Double)1/(Double)2) = 0.5
(Double)(1/2) = 0.0
Why the last operation is 0? :S
Kind regards.
Because 1 and 2 are integers. The result is 0. If you cast that to a double, it's still 0. This question was asked just a couple of days ago.
If you divide two ints, then the result will also be int. So 1/2 gives you a zero which is an integer. Then you are casting 0 to double, which is still zero.
Everyone's given you the correct answer so far, I'm adding this so other readers don't miss it in the comments.
Use the same rule as regular math. Inner Parenthesis first. So in the first example, the 1 is casted to a double before the division occurs, making the result a double (division of int and double results in double). This rings true if it is (Double)1/2 or 1/(Double)2. So in the last example, (Double)(1/2), the (1/2) is performed first, int on int, resulting in int. Then the (Double) casts it to a Double. Hope this not only helps you but anyone else curious about this question. I myself have had many times where I had a long equation and literally had to cast each parameter of the equation to a double.
Try (double)(1.0/2.0) - that will give the answer you expect.
As already written, the problem is the type. You can use suffixes to make sure the type is correct:
1d/2d=0.5
(Double)1/2 = 0.5
(Double)1/(Double)2 = 0.5
(Double)((Double)1/(Double)2) = 0.5
(Double)(1/2) = 0.0
In first three cases You cast the integer value (thas is default type for number when you do not use suffix or does not contain dot ) to Double and then do the division with an integer value in first case and with double for 2 and 3, in contrast to last (4) case where the brackets change the order of operation first You divide two integers, and after that cast the result to Double.

how to convert decimal points? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
i have value of 1.564 and 1.565 i need round this value to 1.56 and 1.56,which function suitable for this in c#.
do a multiply by 100 followed by a floor and followed by a divide by hundred. I am sure that there is a better way of doing it though
Math.floor(n*100)/100
To remove the less significant digits (1.348 -> 1.34):
Math.Floor(number * 100) / 100;
To round the number to two decimals:
Math.Round(number, 2);
To represent it as a string, for display:
number.ToString("#.00");

Categories

Resources