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

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

Related

Math.Sqrt(2/3) returns 0 [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 6 years ago.
I am really confused why this is happening, my code:
double x = Math.Sqrt(2/3);
MessageBox.Show(x.ToString());
Displays 0.
The answer is
0.8164, I know I will also have to use Math.Round to round this up, but for the moment the issue is I'm getting 0
The problem is caused by automatic integer evaluation of the numbers. Use:
double x = Math.Sqrt(2f/3f);
MessageBox.Show(x.ToString());
2 / 3 is an integer operation, what you want is 2.0 / 3 which means I want to use floating point numbers.
What you consider an Intereger is different from what you know from Maths. In programming languages it means that a result of an int-operation is allways an integer in itself.
In your example 2 / 3 is an integer-operation which means the result is rounded down to the nearest integer, which is zero. To avoid this indicate that at least one of your operands should be treates as some floating-point value, either using 2.0 or 2f (alternativly 3.0 or 3f).

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# double - why is the answer always 0? [duplicate]

This question already has answers here:
Why returns C# Convert.ToDouble(5/100) 0.0 and not 0.05
(7 answers)
Closed 8 years ago.
I have a double that when assigned is always coming back as 0.0, but should not happen.
The code I have is
double test = (27096140 / 27216140);
The answer to this should be 0.9955....
I am not using the double primitive type correctly?
Cheers
Edit
Of course, I was using int and no floating points.
Knew it would be a matter of common sense.
You are using integer division, you should use floating-point division:
double test = (27096140.0 / 27216140);
You are actually performing integer division here because the 2 numbers are whole. That forces the result of the calculation to be int which truncates the precision so you end up with 0.
You need to tell the compiler you are actually working with floating-point numbers and not int's, just adding a floating point to one of the numbers should fix this e.g.
double test = (27096140.0 / 27216140);
Alternatively, you could actually declare the numbers as doubles
double a = 27096140;
double b = 27216140;
double test = (a / b);
Or even cast the number in the calculation
double test = ((double)27096140 / 27216140)
Beacuse you're using integers instead of doubles. Use:
double test = (27096140.0d / 27216140.0d);
The numbers 27096140 and 27216140 are ints, so the result of the division is also an int: 0.
Then this is cast to a double: 0.0

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");

How to round to a specific decimal accuracy? [duplicate]

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);

Categories

Resources