displaying decimals on lcd.

I want to show voltage on a lcd display. The variable shows for example 423. It should display 4.23v That works, but when the variable shows for example 405, the display shows 4.5v. Don't know whats going wrong here, here is my displaying code...

lcd.print(voltage / 100,DEC); lcd.print("."); lcd.print(voltage % 100,DEC); lcd.print("v");

Another related question... Is there a way to clear an lcd line, only when it's changed? The lcd.clear() make the display go darker, it refreshes to fast. When I want to show for example 80%, when it gets 100% and than 80% again, it shows 80%%. I can print spaces like lcd.print("% "); but it does not looks like the right way to do this.

you could do

lcd.print(voltage / 100,DEC);
lcd.print(".");
if ((voltage%100)<10) lcd.print('0');
lcd.print(voltage % 100,DEC);
lcd.print("v");

Ultimately if you want total control of a string output you should look into the C function sprintf. That’s the way to go. Just google it.

Thanks for the tip, but thats looks to complicated for me. I'am just a beginner with Arduino. Can someone show me a code that solves my problem as simple as posible?

mwhens: Thanks for the tip, but thats looks to complicated for me. I'am just a beginner with Arduino. Can someone show me a code that solves my problem as simple as posible?

So 4 lines of code you posted is simple enough for you but 5 lines is too complicated? :roll_eyes: If you understand the 4 lines of code you posted you should have no trouble understanding the line I just added. It says, if the number is less than 10 then print a zero so you won't see 4.05 printed like 4.5 again.

So stupid of me, I did not see that you added a line. Understanding the C function sprintf looked to complicated for me. I understand your added code, smart, thanks!

don’t know if serial.print supports float … give this a try

float v = voltage/100.0;
lcd.print(v, 2);  // 2 = 2 decimals
lcd.print("v");

Three lines of source code (but look how much bigger the sketch probably got due to float math!)

That’s alright. Sprintf can certain wait. Just remember if you want 3 digits after the decimal point, you will need another line to generate additional zeros in case you have 4.005, like:

if ((voltage%1000)<10) lcd.print(‘0’);

That’s alright. Sprintf can certain wait. Just remember if you want 3 digits after the decimal point, you will need another line to generate additional zeros in case you have 4.005, like:

if ((voltage%1000)<10) lcd.print(‘0’);

Think you need to add more lines for three digits, as you need to handle each digit separately…

@liudr:
Can you post the whole code for 3 decimals? Try 4.105