So:

C#

Copy Code

const double newValue = Number / 20.0; double outOfTwenty = 0.0; if (newValue >= 0.0) outOfTwenty = Math.Ceiling(NewValue); else outOfTwenty = Math.Floor(NewValue);

See more:

Hello everyone!

I am currently trying round figure out how much a value out of 20 is. For instance,

-5 out of 20 would be -1.

5 out of 20 would be 1.

25 out of 20 would be 2.

-25 out of 20 would be -2

However, when I do that:

-5 returns as 0. Which is normal, however, I need the negative value -1. Does anyone have any ideas? This is driving me insane, and doing this manually:

Would not make any logic for me, and not for my program either. Please help!

I am currently trying round figure out how much a value out of 20 is. For instance,

-5 out of 20 would be -1.

5 out of 20 would be 1.

25 out of 20 would be 2.

-25 out of 20 would be -2

However, when I do that:

C#

Copy Code

int newValue = (int)Number / 20

-5 returns as 0. Which is normal, however, I need the negative value -1. Does anyone have any ideas? This is driving me insane, and doing this manually:

C#

Copy Code

if(Number.StartsWith "-")

Would not make any logic for me, and not for my program either. Please help!

Comments

For positive numbers use Math.Ceiling Method (Double)[^] and for negative numbers use Math.Floor Method (Double)[^].

So:

So:

C#

Copy Code

const double newValue = Number / 20.0; double outOfTwenty = 0.0; if (newValue >= 0.0) outOfTwenty = Math.Ceiling(NewValue); else outOfTwenty = Math.Floor(NewValue);

Comments

Right, a 5. You could also have mentioned Math.Round, for comparison.

—SA

—SA

-16 returns to 0?

-16 / 20 = -0.8?

-16 / 20 = -0.8?

Math.Floor(-0.8) should be -1.0

if (Number > 0)

{

Number = Math.Ceiling(Number );

}

else

{

Number = Math.Floor(Number );

}

{

Number = Math.Ceiling(Number );

}

else

{

Number = Math.Floor(Number );

}

:S

You need to work with double, not int. Conversion to int always loses the decimal place. Thus:

int x = -16 / 20; // x will be 0

double y = (double)-16 / (double)20; // y will be 0.8.

Note that at least one of the operands in the expression also need to be double (in this case cast to double) - it doesn't hurt to ensure both are, since the compiler will do the cast anyway. Otherwise integer arithmetic will be used and then simply assigned to the double. I.e.

double z = -16 / 20; // z will also be 0

Specifying the decimals part also causes it to interpret the value as a double, hence the answer is right.

double y = -16 / 20.0; // y will be 0.8.

By the way, I don't understand your question either, because 5 / 20 will also give the answer of zero, not one.

Regards,

Ian.

int x = -16 / 20; // x will be 0

double y = (double)-16 / (double)20; // y will be 0.8.

Note that at least one of the operands in the expression also need to be double (in this case cast to double) - it doesn't hurt to ensure both are, since the compiler will do the cast anyway. Otherwise integer arithmetic will be used and then simply assigned to the double. I.e.

double z = -16 / 20; // z will also be 0

Specifying the decimals part also causes it to interpret the value as a double, hence the answer is right.

double y = -16 / 20.0; // y will be 0.8.

By the way, I don't understand your question either, because 5 / 20 will also give the answer of zero, not one.

Regards,

Ian.

Okay, so let's suppose everything is positive.

Why does (15) return as 0?

int Value = (int)Math.Ceiling((double)(Math.Abs(15) / 20));

Why does (15) return as 0?

int Value = (int)Math.Ceiling((double)(Math.Abs(15) / 20));

15 / 20 = 0.75

Sorry for the abs, I had a negative number inside it like a few seconds ago.

And then rounding 0.75 up would equal to one? Even if it's just more than 0.5?

Because, as I said, it's doing integer arithmetic so loses everything after the decimal point.

15 / 20 = 0

15 / 20.0 = 0.75

Ian.

15 / 20 = 0

15 / 20.0 = 0.75

Ian.

Much obliged!

You would need to do this instead:

int value = (int)Math.Ceiling(Math.Abs(15) / 20.0);

I've left your call to Abs so you can see the difference.

int value = (int)Math.Ceiling(Math.Abs(15) / 20.0);

I've left your call to Abs so you can see the difference.

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

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900