Pages: [1]   Go Down
Author Topic: Converting to decimal on LCD display  (Read 1071 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you have to add ".0"
Code:
Serial.print(2.0/ 100.0);
Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Too easy!
So will all maths work like that?
Logged

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 17, 2011, 01:06:03 am by eddiea6987 » Logged

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13676
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7189
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
lcd.print(number/100); // integer part
lcd.write('.'); // Print decimal point
if ((number%100)<10) lcd.write('0');// leading zero
lcd.print(number%100);
Logged


Pages: [1]   Go Up
Jump to: