Pages: [1]   Go Down
Author Topic: Converting float into 2 integers  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1628
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pete

Thanks for your help, that worked perfectly!
Logged

Pages: [1]   Go Up
Jump to: