+2 votes
by
Can't understand why is different. example below.
float val = 0.94;
(int)(val*100)
The result is 93 ?

float val = 0.94;
float val1 = val*100;
(int)(val1)
The result is 94 ?

2 Answers

0 votes
by
 
Best answer
Correct. Read about how to construct the float. Take a look at the decimal. Perhaps they are suitable for your purposes.
0 votes
by
Has not reproduced. Wrote:
static int Val1(){
float val = 0.94f;
return (int)(val*100);
}
static int Val2() {
float val = 0.94f;
float val1 = val*100;
return (int)(val1);
}
Both functions returned 94. As you get 93?
by
Nikita Ignatenkov : Managed to get 93. Here it is:
static int Val3(){
float val = 0.94f;
return (int)((double)val*100);
}
In fact, suspicious that you have a place float val=0.94; I she writes
Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
(compiler VS 2013). Probably different settings. Or different languages.
by
Took working code from the project . Can't tell you why you have not reproduced, it is possible to environment differs)
...