How to convert float to string

Hi all,
I need some advice, I am solving problem how to display on LCD floating point variable value in desired format. I did not found any funcion which I could convert float variable to text (string) which I should be able to play with and display it as I want. Expecting that I have number from 0 to 99.999999... but I want to display only two positons after decimal, it means 99.99. I am not able to cut redundant digits for simple lcd.print() function ...

Any idea how to do it?

I am not able to cut redundant digits for simple lcd.print() function ...

Nor are we able to offer suggestions without knowing what lcd is an instance of. If it is an instance of something that derives from Print, then you should look at the documentation for the Print class. There is a way to limit the number of digits after the decimal point, and it is remarkably easy to use.

If it doesn't, than add 0,005 to the value, use dstorf() to convert to a string, and put a NULL in the appropriate place in the resulting array. Locate the decimal point, using strstr().

PaulS:

Nor are we able to offer suggestions without knowing what lcd is an instance of

I am not sure what exactly do you mean with 'instance', but lcd.print is function from standart LiquidCrystal library .... is it what you asked?
When I have e.g. float variable myvar with actual value 99.99999, then

lcd.print(myvar);

prints at actual cursor position text "99.99999" ....

PaulS:

... use dstorf() to convert to a string ...

... I did not found it at arduino reference page ... please which library is it part of, or when can I find it?

Thanks Lumptom

lumptom:
I did not found any funcion which I could convert float variable to text (string) which I should be able to play with and display it as I want.

You may want to take a look at String - object from the reference page. http://arduino.cc/en/Reference/StringConstructor The String() object itself can convert most numbers directly into a useable string.

A simple google search of "Arduino lcd print float" led me to a relevant forum topic:

I tried this way, but getting error during verification "call of overloaded 'String(float&)' is ambiguous" ... dont know why ...

this is sample code i tried

float myvar = 12.3456789;

void setup() {
}

void loop() {
  String mystring = String(myvar);
}

Arrch:
A simple google search of "Arduino lcd print float" led me to a relevant forum topic:

Arduino Forum

thanks ... but how does it solve my problem? I am able to display whole variable with simple lcd.print(myvar), but that is not what I want .... I need to display only 2 digits after decimal, so I need to play with float as with text and cut it ....

Does this give you something to go on ?Serial.println(1.23456, 2) gives "1.23"
This will work for the LCD too if the lcd library that you are using is derived from the print library, hence the earlier question as to what lcd was an instance of.

UKHeliBob:
Does this give you something to go on ?Serial.println(1.23456, 2) gives "1.23"
This will work for the LCD too if the lcd library that you are using is derived from the print library, hence the earlier question as to what lcd was an instance of.

ahaaaaaaaa ..... I started to uderstand. Sorry, but I am quite newbie with arduino and I never programed in C++ and don't know much about classes and instances and whatever, so you need to talk to me like to idiot, step by step or by very very simple examples XD.

Now I rewrite my sample code and it really works

#include <LiquidCrystal.h>

LiquidCrystal lcd(8,9,4,5,6,7);
float myvar = 12.3456789;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(myvar,2);

}

void loop() {
}

thanks to all of you guys

one more question ..... I tried to find at arduino reference page something about print library, but wasn't succesful ..... could you point me to some print reference so I could learn more about it's possibilities?

many thanks and sorry for maybe stupid questions ...

You will probably get most of what you need for here Serial.print() - Arduino Reference

There are other ways to format data before printing, notable by using the sprintf function. This allows you to format data and put it into a buffer array to be output. A Google search or a search for sprintf on this forum will give you examples.

many thanks Bob