Hey,
Trying to convert a decimal number (temperature) to individual integer variables.The temperature could range from 0.0 to 99.9, and is in 0.1 increment size.
For example: double temperature = 45.6; int digOne = 4; int digTwo = 5; int digThree = 6;
I made an experimental sketch-up of what I need in c++. It's pretty easy to see how I tried to do it, but it didn't work.
#include <iostream>
using namespace std;
int main()
{
double temperature = 12.3;
int digOne = 0, digTwo = 0, digThree = 0;
//digOne
temperature/=10;
digOne = temperature;
//digTwo
temperature = (temperature-digOne)*10;
digTwo = temperature;
//digThree
temperature = (temperature-digTwo)*10;
digThree = temperature;
cout << " \n" << digOne << " \n" << digTwo << " \n" << digThree << " \n";
system ("pause");
return 0;
}
Because I'm trying to glitch a double to integer conversion, it doesn't work properly all the time. And I can't seem to be smart enough to get the modulus working right.
Here's a list of outputs I get from various temperature inputs:
12.3 > 1 2 2 <incorrect
99.9 > 9 9 9
81.3 > 8 1 2 <incorrect
24.5 > 2 4 5
10.0 > 1 0 0
11.1 > 1 1 0 <incorrect
00.0 > 0 0 0
63.7 > 6 3 7
29.8 > 2 9 8
02.1 > 0 2 1
The code when run in the arduinoIDE actually returns a different result for incorrect numbers. But I believe it's the same numbers that they're getting wrong, so fixing the code in either environment should fix it globally.
Could someone please help me figure this one out?
Thanks.
P.S: The input could actually be a negative number up to -9.9, but I'm not too concerned with that at the moment. Although if someone could incorporate that into the calculation to return a nan or something, that'd be great. I can design a work around for nan's to result with '-' being displayed.