Go Down

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

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


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
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy