C# Is Rounding Down Divisions by Itself

C# is rounding down divisions by itself

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.

C# How to divide decimals without implicit round up

You're going to have to implement long division yourself. There is NO way to finagle the built-in division into doing this for you (hint: you can't distinguish between 2m / 3m and .6666666666666666666666666667m / 1m or 6666666666666666666666666667m / 10000000000000000000000000000m for that matter).

Why do these division equations result in zero?

You're using int/int, which does everything in integer arithmetic even if you're assigning to a decimal/double/float variable.

Force one of the operands to be of the type you want to use for the arithmetic.

for (int i = 0; i <= 100; i++)
{
decimal result = i / 100m;
long result2 = i / 100;
double result3 = i / 100d;
float result4 = i / 100f;
Console.WriteLine("{0}/{1}={2} ({3},{4},{5}, {6})",
i, 100, i / 100d, result, result2, result3, result4);
}

Results:

0/100=0 (0,0,0, 0)
1/100=0.01 (0.01,0,0.01, 0.01)
2/100=0.02 (0.02,0,0.02, 0.02)
3/100=0.03 (0.03,0,0.03, 0.03)
4/100=0.04 (0.04,0,0.04, 0.04)
5/100=0.05 (0.05,0,0.05, 0.05)

(etc)

Note that that isn't showing the exact value represented by the float or the double - you can't represent 0.01 exactly as a float or double, for example. The string formatting is effectively rounding the result. See my article on .NET floating binary point for more information as well as a class which will let you see the exact value of a double.

I haven't bothered using 100L for result2 because the result would always be the same.

Rounding down to 2 decimal places in c#

The Math.Round(...) function has an Enum to tell it what rounding strategy to use. Unfortunately the two defined won't exactly fit your situation.

The two Midpoint Rounding modes are:

  1. AwayFromZero - When a number is halfway between two others, it is rounded toward the nearest number that is away from zero. (Aka, round up)
  2. ToEven - When a number is halfway between two others, it is rounded toward the nearest even number. (Will Favor .16 over .17, and .18 over .17)

What you want to use is Floor with some multiplication.

var output = Math.Floor((41.75 * 0.1) * 100) / 100;

The output variable should have 4.17 in it now.

In fact you can also write a function to take a variable length as well:

public decimal RoundDown(decimal i, double decimalPlaces)
{
var power = Convert.ToDecimal(Math.Pow(10, decimalPlaces));
return Math.Floor(i * power) / power;
}

Why is my float automatically rounding and how do i get it to stop

Your calculation is being done in integer type since both the operands are of int type

cast or mark atleast one of the operand as float.

float pageCount = 10/6f; //6f specifying 6 as float

or

float pageCount = ((float) 10)/6;

In your current form, both operands are of integer type and their division results in integer value that is why you get 1 not 1.666

How to integer-divide round negative numbers *down*?

WARNING: this post produces incorrect results for input with a=-1. Please see other answers. -Cam


[c++]

This is probably what you meant by 'kludgey', but it's what I came up with ;)

int divideDown(int a, int b){
int r=a/b;
if (r<0 && r*b!=a)
return r-1;
return r;
}

In the if statement, I put r<0 - however I'm not sure if that's what you want. You may wish to change the if statement to

if (a<0 && b>0)

which would be consistent with your description "Seems like whenever I divide a negative int by a positive int ".

Is there a Method for Math division without Rounding errors?

There is actually a way in arithmetics to go around precision problem, without fractions. It can be very hard to implement though, if your formula is dynamic (not hardcoded). Otherwise, you can just rearrange your operations to be more precise for specific domain of numbers, for example:

(x + 1) - x 
  • with big floats it will become somewhat 0
  • with small floats it will
    become somewhat 1 (small floats are more precise than big one)

So rearanging it like this, will give correct result in both:

(x - x) + 1

I know example is trvial, but for specific operations you should choose specific rearangement, for example knowing that floats closer to zero are MUCH more precise, you can just work with them only. In my example rearangement is such that I want to minimize impact of variable, so I tie them closer to each other, to destroy their bloating to big, more inprecise floats. For example, I will win this out if I had something like this:

x^1.05 + 1 - x^1.01

The trivial dynamic approach is generally to sort operations in ascending order - from lower floats operations, to bigger floats. Variables x,y,z,etc can be big or low, so here is a problem of sorting - you sort each time you pass those variables inside your formula, and it will give you best precision. Or you hardcode different permutations of rearrangement for different inputs.

Here is article: https://books.google.ru/books?id=KJORYTHOxbEC&pg=PA390&lpg=PA390&dq=rearrange+math+operations+for+precision&source=bl&ots=y8E8fjdrYy&sig=ACfU3U1vfkonygDnLJhSCK3qh0C2kaXK3w&hl=en&sa=X&ved=2ahUKEwiv6v2Xs4PqAhXGzaQKHTTUDlwQ6AEwAHoECAgQAQ

Wrong calculation c#

This is due to rounding to int values because both values seem to be ints. Make sure you have at least one decimal number.

Make sure you devide with a float or double by adding .0, for example:

double numofpages = listcoinslist.Count / 100.0;



Related Topics



Leave a reply



Submit