Reputation: 184
I have a condition like:
if X is between (1 to 4) ----- value 0
if X is between(4 to 8) ----- value 1
if X is between(8 to 12) ----- value 2
if X is between(12 to 16) ----- value 3
and so on....
What is the best way to do this in C#?
Upvotes: 0
Views: 126
Reputation: 14628
Integer division in C# truncates, so if the number you are talking about is an integer and is assigned to the variable myInt, all you have to do is write an expression like this:
(myInt -1) / 4
.
And if the number is a float, casting to int truncates, so just cast it like this:
((int) myFloat - 1) /4
Upvotes: 1
Reputation: 25352
Try like this
int x=5;
int num = (int)Math.Ceiling(x/4.0)-1;
Console.WriteLine(num);
Upvotes: 1