So. I try code for extracting integer and fraction part of my data.
//#include <math.h>
#include <Wire.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 4, 5, 6, 7, 8);
void setup(){
lcd.begin(16, 4);
pinMode(9, OUTPUT);
Serial.begin(9600) ;
}
void loop()
{
double a = -278.789456; // test value
int val=0;
long frac=0;
double q=0;
val=int(a); //integer part
if(val >= 0) //fraction part
frac = (a - int(a)) * 1000000;
else
frac = (int(a)- a ) * 1000000;
q=(frac*0.000001)+val;
lcd.setCursor(0,0);
lcd.print(a,6);
lcd.setCursor(0,1);
lcd.print(val);
lcd.setCursor(0,2);
lcd.print(frac);
lcd.setCursor(0,3);
lcd.print(q,6);
}
I have errors:
-
Lost precision in fraction part.
data is -278.789456
On LCD i see -278.789459
integer part -278 it's ok
fraction part is 789459 - wrong -
Incorrect recovery from integer and fraction part.
I have -277.210540 on LCD
I think - my recovery formula - not correct.
Please help with this troubles.
Thanks