lcd.print() two decimal places

Hello, I am trying to get the arduino to print to a precision of 2 decimal places.

Here is a slimmed down example of what my code looks like.

...

sensorValue = analogRead(sensorPin);
sensorValuePercentage = (sensorValue - 23) / 10;

...

lcd.print(sensorValuePercentage);

...

See if your LCD library implements the sprintf() method.

Thank you, I have been looking into float but cannot find a good tutorial on how to properly use it. All of my attempts so far have failed...
Do you have a good example?

I have seen that page. Cannot seem to implement it correctly :frowning:

This is the code i am using. It is simple enough.

#include <LiquidCrystal.h>
LiquidCrystal lcd(52, 53, 50, 51, 48, 49);

int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
int sensorValuePercentage = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  lcd.clear();
  lcd.begin(20, 4);
  lcd.print("... loading");
  delay(200);
}
void loop() {
  {
    sensorValue = analogRead(sensorPin);
    sensorValuePercentage = (sensorValue - 23) / 10;
    
    digitalWrite(ledPin, HIGH);
    delay(sensorValue);
    digitalWrite(ledPin, LOW);
    delay(sensorValue);
  }
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Potentiometer Value");
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.setCursor(5, 1);
  lcd.print("ohms");
  lcd.setCursor(0, 2);
  lcd.print(sensorValuePercentage);
  lcd.setCursor(5, 2);
  lcd.print("%");
  }
}

NEVERMIND, I figured it out. Thanks for the help!

Here is the modified code in case someone else has a similar issue...

#include <LiquidCrystal.h>
LiquidCrystal lcd(52, 53, 50, 51, 48, 49);

int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
float sensorValuePercentage;

void setup() {
  pinMode(ledPin, OUTPUT);
  lcd.clear();
  lcd.begin(20, 4);
  lcd.print("... loading");
  delay(200);
}
void loop() {
  {
    sensorValue = analogRead(sensorPin);
    sensorValuePercentage = (float)(sensorValue - 23) / 10;
    
    digitalWrite(ledPin, HIGH);
    delay(sensorValue);
    digitalWrite(ledPin, LOW);
    delay(sensorValue);
  }
  {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Potentiometer Value");
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.setCursor(5, 1);
  lcd.print("ohms");
  lcd.setCursor(0, 2);
  lcd.print(sensorValuePercentage);
  lcd.setCursor(5, 2);
  lcd.print("%");
  }
}

Thinking outside the box you could use an integer to hold the value multiplied by 100, then print the value / 100 followed by a decimal point followed by the value mod (%) 100.

Example

301.34 would become 30134. 30134 / 100 in integer maths would become 301 and 30134 mod 100 would become 34.

pluggy:
Thinking outside the box you could use an integer to hold the value multiplied by 100, then print the value / 100 followed by a decimal point followed by the value mod (%) 100.

Example

301.34 would become 30134. 30134 / 100 in integer maths would become 301 and 30134 mod 100 would become 34.

... which is what the Print::print(double n, int digits) method that the lcd library contains does.