Go Down

Topic: Converting to decimal on LCD display (Read 1 time) previous topic - next topic

Packhorse


I need to display a value on a LCD display as a decimal. it will be x/100 and the result will be between 0.00 and 2.00
If I try
lcd.print(x/100);
it does not work and simply show 1

Thanks

eddiea6987

you have to add ".0"
Code: [Select]
Serial.print(2.0/ 100.0);
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Packhorse

Too easy!
So will all maths work like that?

eddiea6987

#3
Sep 17, 2011, 08:04 am Last Edit: Sep 17, 2011, 08:06 am by eddiea6987 Reason: 1
well that is for floats, 
integers will be fine without the decimal.
take a look at the data type section
http://www.arduino.cc/en/Reference/HomePage
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

robtillaart

Two remarks:

Be aware that using floats is relative expensive in term of memory and speed.
Doing it with int 's is possible but takes a few lines of code as you need to "calculate every digit simultaneously.

// you can add a 2nd parameter to the print statement to idicate the number of decimals, default this is 2.
Serial.print(2.0/ 100.0, precission);
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr

OP, say you already have a number between 0 and 200, and you want to display between 0.00 and 2.00, here is what the extra lines rob was referring to:
Code: [Select]

lcd.print(number/100); // integer part
lcd.write('.'); // Print decimal point
if ((number%100)<10) lcd.write('0');// leading zero
lcd.print(number%100);

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy