# (int)(100*X) question

Ok

i have a float variable "X"
which is equal to 1.50000
when multiplying by 100 i have 150.00000
but if i want to convert to int with (int) i have 149!

this also appens with other values as well...
can anyone tell me why?
thanks

-Flat

• : Ok
:
: i have a float variable "X"
: which is equal to 1.50000
: when multiplying by 100 i have 150.00000
: but if i want to convert to int with (int) i have 149!
:
: this also appens with other values as well...
: can anyone tell me why?
: thanks
:
: -Flat
:
When you convert your float number to int, the decimal is cut off therfore rounding occurs. Int numbers cannot hold a decimal place, only whole numbers. You may wanna do some reading up on inager variables. <<<WICKET>>>

URL:http://www.wicketware.com

• : Ok
:
: i have a float variable "X"
: which is equal to 1.50000
: when multiplying by 100 i have 150.00000
: but if i want to convert to int with (int) i have 149!
:
: this also appens with other values as well...
: can anyone tell me why?
: thanks
:
: -Flat
:

Is it part of a program? The problem should be in some other part of your program. Maybe problems with rounding or an impropriete cast that you are using.
int x = 1.50000
printf("%d
", (int)(100*x))
If you use it like this then you get 150 and not 149

• : : Ok
: :
: : i have a float variable "X"
: : which is equal to 1.50000
: : when multiplying by 100 i have 150.00000
: : but if i want to convert to int with (int) i have 149!
: :
: : this also appens with other values as well...
: : can anyone tell me why?
: : thanks
: :
: : -Flat
: :
:
: Is it part of a program? The problem should be in some other part of your program. Maybe problems with rounding or an impropriete cast that you are using.
: int x = 1.50000
: printf("%d
", (int)(100*x))
: If you use it like this then you get 150 and not 149
:
:
:
:
:
:
:
:
:
:
Hi and happy new year (and century and ...),

Perhaps X is not realy equal to 1.50000 but to 1.49999999999999.
In that case, if you want to see X, you will see 1.50000.
To see if you are in that case, you can do "cout<<double(double(X)-double(1.5))<<endl;"
if you have 1E-10 (or an exponent greater than 10), you are in that case. If you have exactly 0 I can't help you.

Bye,

Vincent.