Page 1 of 1

Can't print floats??

Posted: Mon Jan 26, 2015 7:03 am
by ngp99
I am working on the Jaycar water flow gauge. I note the section

Code: Select all

    //  Because we want to output a
    // floating point value and print() can't handle floats we have to do some trickery
    // to output the whole number part, then a decimal point, then the fractional part.
    unsigned int frac;
    
    // Print the flow rate for this second in litres / minute
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
I print floats from my DS18B20 temperature sensor OK, so why can't I do that with the flow meter? Is this software out of date? Or is it actually quicker to go around the long way?

Re: Can't print floats??

Posted: Mon Jan 26, 2015 9:55 pm
by andrew
That was published in early 2012, from my vague memory there was an issue with floats at the time so that was the simplest solution. It still works however you can of course change it if required.

Re: Can't print floats??

Posted: Tue Jan 27, 2015 5:29 am
by ngp99
Thank you. I found an old version I had from 2013, and realised that I had already changed it then to print floats and it works fine.