Oled print only 1 desimal

Hi

I want to present temp with 1 desimal
Code under gives 25.12 Celsius

display.setCursor(80, 25); display.print(myPayload.DHTtempKube);

I have tried "%.1f"

display.setCursor(80, 25); display.print("%.1f", myPayload.DHTtempKube);

but this don't compile:

Error:

ArduinoData\packages\esp32\hardware\esp32\2.0.6\cores\esp32/Print.h:91:12: note: conversion of argument 1 would be ill-formed:
Receiver_v32:193:49: error: invalid conversion from 'const char*' to 'long long unsigned int' [-fpermissive] display.setCursor(80, 45); display.print("%.1f", myPayload.pakkeNr);

Anyone who can give me a tip? Was hoping to add code per display.print line, not make an conversion up front.

M

Did you try

Hi

I mixed, flota vs int. Soultion came from you @anon56112670

correct is:

display.setCursor(80, 15); display.print(myPayload.vekt, 1);

Alternatively

display.printf("%.1f", myPayload.DHTtempKube);

However, only some types of arduino support use of .printf(), like ESP8266 & ESP32.

Other types of Arduino like Uno do not, but you can use sprintf() instead

char buffer[20];
sprintf(buffer, "%.1f", myPayload.DHTtempKube);
display.print(buffer);

An advantage of printf() and sprinf() is that you can specify a minimum field with:

display.printf("%2.1f", myPayload.DHTtempKube);

This will ensure that a space is printed before the first digit, if the value is under 10. This can help with aligning a column of numbers on your display.

You can also use

display.printf("%02.1f", myPayload.DHTtempKube);

which will print an extra 0 if the value is under 10.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.