Go Down

Topic: Converting to decimal on LCD display (Read 2073 times) previous topic - next topic


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
it does not work and simply show 1



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"


Too easy!
So will all maths work like that?


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
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"


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)


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
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

Go Up