15,665,229 members
1.00/5 (1 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 20-Dec-22 23:05pm

## Solution 3

Quote:

There is nothing to solve here. 5/2 is an integer division with an integer result.
If you want a floating point division, try:
C#
```double up = 5/2.0;
// or
double up = 5.0/2;```

BillWoodruff 21-Dec-22 6:38am
+5
Patrice T 21-Dec-22 15:35pm
Thank you

## Solution 2

you can use / and Ceiling ..

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

## Solution 1

just use int
C#
```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

## Solution 5

float a = 5f; //decimal a = 5m; //int a1 = 5;
float b = 2f; //decimal b = 2m; //int b1 = 2:
double c = a % b;
double d = a / b;// duoble d = (double) a1 / b1;

Console.WriteLine(\$"divide {d} {c}");

v4
Richard Deeming 21-Dec-22 5:12am
As already clearly explained in the existing solutions, which were posted nearly five years ago.

Stick to answering new questions unless you have something new and interesting to add to the discussion.