Float format printing to LCD

Hello,

I need to print float numbers to LCD by controlling decimal numbers and total size number printed.

Using lcd.print( FloatNumber, Precision ) only decimal numbers size can be changed.
I need something equivalent to printf format options:

printf("%7.3f\n",6.2145);
printf("%7.3f\n",112.4);
printf("%7.3f",1.01);

OUTPUT:
  6.215
112.400
  1.010

I tried to use sprintf but unfortunately arduino implementation doesn't support float numbers.
There is no simple way to set the float format printing to LCD with arduino?

Thanks

dtostrf

Thanks,
I am looking for dtostrf.h library. It seems to not be included in arduino folders.
#include <avr/dtostrf.h> doesn’t work.

A dtostrf reference:
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42

http://www.hobbytronics.co.uk/arduino-float-vars

There are several ways. By far the simplest is to print blanks " " before, according to the number of significant digits. That is all I do, but 95% of my data has two sig digits and never any more. If your numbers are all over the place, you might use dtostrf.
http://www.hobbytronics.co.uk/arduino-float-vars
it doesn't need a library.

You don't need to #include anything for dtostrf() to work

I first tried to use dtostrf directly but Arduino 1.8.4 didn't recognize it. Why?

Why?

You did not use it correctly? Show the code and the error message and maybe we can help.

you can use sprintf...

void setup() 
{
  Serial.begin(9600);
  double num = 123.45678;
  char displayString[17] = "";
  sprintf(displayString, "value:%7d.%d%d", (int)num, int(num*10)%10, int(num*100)%10);
  Serial.println(displayString);
}

void loop() 
{
}
1 Like

I think there was a problem with libraries. I have reinstalled Arduino IDE now dtostrf works correctly.
Thanks to all.

My easy soulition is (in multiple i2cLCDlibrary):

when temp1 is float,
example value: 20.25

The code:

lcd1.print(int(temp1),DEC); //The "truncate" is "20"
lcd1.print(".");//The decimal point
lcd1.print(int(temp1100)-(int(temp1)100)); //The Frac is 20.25100{2025} - 20100{=2000} =25

AdrianCorner:
My easy soulition is (in multiple i2cLCDlibrary):

when temp1 is float,
example value: 20.25

The code:

lcd1.print(int(temp1),DEC); //The "truncate" is "20"
lcd1.print(".");//The decimal point
lcd1.print(int(temp1100)-(int(temp1)100)); //The Frac is 20.25100{2025} - 20100{=2000} =25

What about 20.09?

("Easy' is not always == "good")

Oh my god… I used with only 1 digit :slight_smile:
Thanks your remark!

lcd1.print(int(temp1),DEC); //The “truncate” is “20”
lcd1.print(".");//The decimal point
if (int(temp1100)-(int(temp1)100)<10) lcd1.print(“0”); // The new lie :smiley:
lcd1.print(int(temp1
100)-(int(temp1)100)); //The Frac is 20.25100{2025} - 20
100{=2000} =25