I have an int that is counting pulses sent to the motor, and i am dividing this int by another number, which would give me a decimal. When ever i try to print the decimal it truncates it. pulses 1 is my counter, so here is my code:
The Serial routine has no support for floating-point value types. Floating-point support tends to take up a lot of room in program space, and calculations are slow, so it was pulled out to save resources. There are a number of threads that have touched on this.
The only way I can think of doing it easily is to treat the number as a string and then split the string by the decimal, and print each part separately with a decimal point in the middle.
whole_num = strtok(number,"."); // Split the string by the token "."
dec_num = strtok(NULL,"."); // Split the rest of the string into the next token
Serial.print(whole_num);
Serial.print(".");
Serial.println(dec_num);
Something like this might work... not tested though. I dont know if there is a strsplit function.
ryanjmclaughlin, you can't just take a number and "treat it as a string" in the way you're suggesting. A floating-point number is encoded very tightly into a fixed number of bytes. Some bits represent the base two exponent, some bits represent the base two mantissa (the precision bits), some bits describe how many bits are reserved for each, and then two bits are for the signs of mantissa and exponent. There is no '.' character in there.
Just for printing purposes I think there's a usefull solution in 1-wire example. You have readings like Tc_100=2134; (which means 21.34°C). You take this number and do:
Whole = Tc_100 / 100;
Fract = Tc_100 % 100;
and then you print it:
lcd.print(Whole);
lcd.print(".");
if (Fract < 10)
{
lcd.print("0");
}
lcd.print(Fract);