Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
Can anyone tell me why the following statement in C# evaluates to an int result 2 instead of 2.5
C#
float f = 5 / 2;
Console.WriteLine(f);

Same code works in C Language and give the output 2.0000 rather than 2.5000
C++
float f = 5/2;
printf("%f",f);

and C++ works same like to C# and gives the output 2
C++
float f = 5/ 2;
cout<<f; 


What I have tried:

I've tried to investigate this on different forums . Its pretty simple concept so I've posted it here to know the expert opinion about this.
Posted
Updated 14-Jun-17 21:00pm
v3

Take a look at this[^] thread, it deals with a similar question.
 
Share this answer
 
It's because you're specifying two integers (5 and 2), not floating point values.

Change the code to (in C#):
C#
float f = 5f / 2f;
Console.WriteLine(f);


In most languages, if you don't specify a decimal pointed value (5 instead of 5.0) the value will be treated as an integer. Integer division will never return a floating point value, so you get 2 instead of 2.5.
 
Share this answer
 
v2
Comments
Ammar Shaukat 14-Jun-17 16:51pm    
means if divide operator has int numbers on both sides, it will treat the whole calculation as integers ( an implicit casting will happen most probably )
Dave Kreskowiak 14-Jun-17 17:48pm    
There is no implicit cast on the right side of the expression. Since you didn't express the 5 and 2 as any floating type, they are interpreted at integers. The only implicit cast made in that statement is when the expression on the right is evaluated and about to be assigned to the variable on the left. Your expression is really this:
    float f = (float)( (int)5 / (int)2 );

which ends up evaluating to this:
    float f = (float)( 2 );

which finally ends up as:
    float f = 2f;


There are various ways of specifying the type of a constant value:
<pre lang=C#">
int c = 2;
float f = 5.0F;
double d = 4.0;
double d = 4.0D;
double d = (double)4;
decimal d = 6.0M;
decimal d = (decimal)6.3;
Hassaan Akbar 14-Jun-17 17:12pm    
i would like to add.. (float)(5/2) will also return 2 because we are typecasting after it has already been internally casted to int.

Some correct ways are,

5f/2
5/2f
5.0/2
5/2.0
(float)5/2



In longer formulas its usually enough to typecast the first calculations following DMAS rule

for example the following formula will return correct result,

(5/2.0 + 3*6.0)(324 + 452)+ (32+ 341/2.0)
In C# by default the / operator returns the result in System.Int32.
C#
System.Int32 answer = 5 / 2;

if you want answer in float or double just do explicit casting.
C#
decimal answerInDeciaml = (decimal)5 / 2;
 
Share this answer
 
v2
C#
float f=5f/2;

try this one by defalut result will be shown in integer values it 5&2 treats as a integer and return integer values.
 
Share this answer
 

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