Angel.King.47
Angel.King.47

Reputation: 7994

Convert a float to two int in c/++

NOT A DUPLICATE: the duplicated mentioned above, returns the decimal part into a float, and not a int

Given I have a float 1.495 How can I convert it into, 1 and 495 given two int variables r and d. Where:

int r == 1;
int d == 495;

I can get the first one using

int r = (int)(1.495f-1l);

But im not sure on how to cast or get the d value.

Note I am not sure of the range of decimal part, the decimal part could be .49 or .495 or .4959

In the case of the value being .0495 or .00495 I am OK with the value being returned to be 495, I didn't think of this originally.

Thanks

Upvotes: 1

Views: 3484

Answers (1)

James Holderness
James Holderness

Reputation: 23001

float f = 1.5;
int r = f;
int d = (f-r)*1000;

Update:

Just to be clear, while this answered the poster's question as originally phrased, it does not solve the problem as its given now.

Upvotes: 5

Related Questions