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:

C#

double up = 5/2.0; // or double up = 5.0/2;

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);

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"

i have try 5/2 or

a = 5 / 2

double up = a;

up = Math.Ceiling(up);

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:

C#

double up = 5/2.0; // or double up = 5.0/2;

Permalink

Share this answer

Comments

BillWoodruff
21-Dec-22 6:38am

+5

Patrice T
21-Dec-22 15:35pm

Thank you

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

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

Permalink

Share this answer

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

Permalink

Share this answer

v2

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}");

Console.Read();

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}");

Console.Read();

Permalink

Share this answer

v4

Comments

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.

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

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