Lama Dingo
Lama Dingo

Reputation: 123

Convert from int to float, possible round up?

C++ conversion:

I recieve information from Hardware in this Form:

x = 564 as int.

I have show this information in float:

xFloat = 56.4 as float

If I do x/10 I always get the wrong result:

56.5 instead of 56.4.

Is this a Round up problem or is it possible that my hardware doesn´t deliver right information?

Upvotes: 2

Views: 194

Answers (2)

TryinHard
TryinHard

Reputation: 4118

Please note that in C++ the default decimals are double.

You can also try to use these:

int x=564;
float f=(float)x/10;    //56.400002    
float f1=x/10.0;        //56.400002
float f11=x/10.0f;      //56.400002
double d=(double)x/10;  //56.400000
double d1=x/10.0;       //56.400000

Upvotes: 3

Amanullah Tariq
Amanullah Tariq

Reputation: 120

You can use code like this

int x = 564;
double num = x / 10.0;

Upvotes: 2

Related Questions