Click here to Skip to main content
14,367,061 members
Rate this:
Please Sign up or sign in to vote.
See more:
{ double up = 5/2;
up = Math.Ceiling(up);

double down = 5/2;
down = Math.Floor(down);

MessageBox.Show("up " + up + " down " + down);
}

'' the result from MessageBox is " up 2 down 2"
'' i want the result is " up 3 down 2"

What I have tried:

i have try 5/2 or
a = 5 / 2
double up = a;
up = Math.Ceiling(up);
Posted
Updated 6-Oct-19 18:07pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

you can use / and Ceiling ..

your problem is
x = 5/2
x is allready 2, not 2.5 as you may suggest. You need to look into that kind of problems with the debugger. to see if the code does what you want it to do

in that case / on int numbers, gives you back .. int numbers, not double as you intend

x = (double)5/2
x will be 2.5 as you use / on a double value

now Math.Ceiling and Math.Floor will work as expected
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
Please help me, to solve this C# 5/2 = 2?

There is nothing to solve here. 5/2 is an integer division with an integer result.
If you want a floating point division, try:
double up = 5/2.0;
// or
double up = 5.0/2;
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

just use int
int down= 5/2; // down=2
int mod = 5%2; // mod =1

int down= 6/2; // down=3
int mod = 6%2; // mod =0
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100