Go Down

Topic: Converting float into 2 integers (Read 357 times) previous topic - next topic

Joseph_hewitt

Hi, I have a temperature in a float and I was wondering, is it possible to turn that into 2 integers?
for example;

float value = 21.74;

Could I have int A equal 21 and int B equal 74?

Thanks.

Arrch

Getting 21 is as simple as assigning value to an int.

Getting 74 can be achieved by multiplying value by 100, then modulus dividing it by 100.

el_supremo

Assuming that A and B are declared as integers:
Code: [Select]
A = value;
// This rounds up the fractional part of "value"
B = (value - A)*100 + 0.5;


Pete

Joseph_hewitt


Assuming that A and B are declared as integers:
Code: [Select]
A = value;
// This rounds up the fractional part of "value"
B = (value - A)*100 + 0.5;


Pete


Thanks for your help, that worked perfectly!

Go Up