Click here to Skip to main content
15,078,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

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:

if(Number.StartsWith "-")

Would not make any logic for me, and not for my program either. Please help!
lewax00 3-May-13 17:06pm
I don't really get what you're trying to do here, but integer division always truncates. So: -5/20 = -0.25 which will truncate to -0, which is the same as 0. As far as checking if a value is negative though, just check if it's less than 0.

1 solution

For positive numbers use Math.Ceiling Method (Double)[^] and for negative numbers use Math.Floor Method (Double)[^].
const double newValue = Number / 20.0;
double outOfTwenty = 0.0;

if (newValue >= 0.0)
    outOfTwenty = Math.Ceiling(NewValue);
    outOfTwenty = Math.Floor(NewValue);
Right, a 5. You could also have mentioned Math.Round, for comparison.
Yvar Birx 3-May-13 17:18pm
-16 returns to 0?

-16 / 20 = -0.8?
André Kraak 3-May-13 17:22pm
Math.Floor(-0.8) should be -1.0
Yvar Birx 3-May-13 17:23pm
if (Number > 0)
Number = Math.Ceiling(Number );
Number = Math.Floor(Number );
Yvar Birx 3-May-13 17:50pm
Ian A Davidson 3-May-13 17:56pm
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.

Yvar Birx 3-May-13 18:31pm
Okay, so let's suppose everything is positive.
Why does (15) return as 0?

int Value = (int)Math.Ceiling((double)(Math.Abs(15) / 20));
Yvar Birx 3-May-13 18:31pm
15 / 20 = 0.75
Yvar Birx 3-May-13 18:32pm
Sorry for the abs, I had a negative number inside it like a few seconds ago.
Yvar Birx 3-May-13 18:34pm
And then rounding 0.75 up would equal to one? Even if it's just more than 0.5?
Ian A Davidson 3-May-13 18:40pm
Because, as I said, it's doing integer arithmetic so loses everything after the decimal point.

15 / 20 = 0
15 / 20.0 = 0.75

Yvar Birx 3-May-13 18:51pm
Much obliged!
Ian A Davidson 3-May-13 18:53pm
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.

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