How to add more digits after decimal point with print and string command

I wanted to print (either to serial or to LCD) more than one variable per program line, in order to avoid multiple print statement lines, and I was able to do that with "String", but then it doesn't let me specify 3 digits after the decimal point in my particular case.
My current project is using two potentiometers to simulate 1 voltage and 1 current and displaying them on the same line, side by side (here is the project link: ESP32 ADC with LCD voltage, current, power, energy - Wokwi ESP32, STM32, Arduino Simulator).
I know it can be done like this:

Serial.print(pot1Voltage, 3);
Serial.print("V, ");
Serial.print(pot2Current, 3);
Serial.println("A");

However, I have found one way to place all that under one print command line, using String data type:

Serial.println((String)pot1Voltage + "V, " + (String)pot2Current + "A");

This works well, but I want to display 3 digits after the decimal point instead of the standard two.
I know how to do it with print command for each variable:

print(pot1Voltage, 3);

but I can't figure out how to do that with the String constructor added to the line, as such line doesn't work in WOKWI:

print((String)pot1Voltage, 3); << Doesn't work, gives an error!

I am knowledgeable ad experienced in electronics, but a beginner in programming, and some of the jargon is not easily understood without enough examples or explanation.
If there is another way to print multiple variables/characters per print instruction, you are welcome to let me know.
I have also read that the String specifier is harder on the MCU, but I don't know enough about that.
You should know that I prefer the simplest and clearest solutions over multiple-line complex mathematical statements.
You may be able to see on my WOKWI project linked above that I try to maintain a simple and clear code, separating lines with space to make them stand out individually.

Serial.println(someFloat, 3);

See the reference: Serial.print() - Arduino Reference

Thank you for a VERY QUICK response, but I have already mentioned in my question that I know how to this with a print command without the String specifier included. I have edited/formatted my question to make it more visible.
But after adding the String specifier to allow for multiple variables and character groups per line, I am unable to specify the number of digits after decimal point (WOKWI compiler shows an error), so that's the point of my question.

Since you are on an ESP32 you can use snprint() to format floating point. Something like this (untested).

char buff[25];  // make enough space
snprintf(buff,(sizeof(buff)-1),"%6.3f V, %6.3f A",potVoltage,potCurrent);
Serial.print(buff);
1 Like

Thank you.
I have tried it in my WOKWI project and it works.
Now I have to study this particular command to understand it and adapt it in the future.
Two questions:

  1. Does this command use more or less MCU resources (such as memory and speed/cycles) than the String specifier?
  2. Do you know if it's possible to do this (specify 3 digits after period) with String?
  1. I would expect less because it doesn't do the dynamic memory allocation that Strings do.
  2. I don't know if there is a way to specify a precision for the String conversion.
1 Like

When converting a float to String, you can specify the number of digits, just like print()

Serial.println(String(pot1Voltage,3) + "V, " + String(pot2Current,3) + "A");

Of course, this begs the questions, why?

2 Likes

DUH, I tried a few things EXCEPT moving the parenthesis. :roll_eyes:
Thank you, your solution works and it answers my question directly.

The reasons why I want this is because:

  1. I like minimizing the code which makes for an easier overview.
  2. There is a more direct relationship between the look/arrangement of the code and the display, at least while I'm learning to code.
  3. Because I don't like repeating same commands multiple times just to get one line of characters.

I do wonder if you know of another way, besides String and snprintf (provided by @oldcurmudgeon above) to print multiple variables or groups of characters with a single line of code.

@blh64 has provided the solution, which involved merely placing parenthesis differently.
I would have also accepted your answer because it is helpful, but I accepted his answer because it answers my exact question and it is a little simpler for the programmer.
Thank you again for your answer.

Still asking why? One call to a library function might involve lots of print() statements, etc. It really seams like you are focusing too much on one line v. solving the problem of printing things. If multiple print statements really bother you that much, put them all in a function and then call that function.

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