How can I increase precision of float display in the Serial Monitor?

Hello,
I am using the Serial Monitor as a troubleshooting tool for my sketch. I must verify my math among other things. The monitor won’t display more than 2 decimal places when reading a float var. I assume it is rounding. I could probably modify it if I just knew where ‘rounding’ is located. I’ve searched Arduino.cc, the forum, googled it and can’t find anything. Has anyone else solved this problem?

Thanks in advance,
-oldfox-

“If it ain’t broke, fix it 'til it is.”

1oldfox:
Hello,
I am using the Serial Monitor as a troubleshooting tool for my sketch. I must verify my math among other things. The monitor won't display more than 2 decimal places when reading a float var. I assume it is rounding. I could probably modify it if I just knew where 'rounding' is located. I've searched Arduino.cc, the forum, googled it and can't find anything. Has anyone else solved this problem?

Thanks in advance,
-oldfox-

"If it ain't broke, fix it 'til it is."

No you just add a second optional argument to the serial.print statement that tells it how many digits to the right of the decimal place you wish to be printed:

Syntax
Serial.print(val)
Syntax
Serial.print(val)
Serial.print(val, format)
Parameters
val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal places (for floating point types)
Parameters
val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

So assuming your val is a floating point variable and you wished to print four digits after the decimal point the statement would look like this:

Serial.print(val, 4);  // print floating point variable showing four digits after decimal point.

Lefty

2 Likes

Thanks Lefty, you are "righty"
And Keith, thanks also. I don't know how I missed that one. Old eyes, broken ears and questionable brain works. :roll_eyes:

retrolefty gave you the fish, I tried to teach you how... 8^)

This is a quirk with the Print method, apparently.

Check the line here : Arduino/Print.cpp at 9d1302307528622ed5d1d2f838bbce1c85301f94 · arduino/Arduino · GitHub

So, basically, if you want to print a float, you declare it as a "double" type, and then call Serial thusly :

Serial.println(double val , int digits);

and you should have what you need

ankitdaf:
This is a quirk with the Print method, apparently.

I don't see how a clearly defined method is a "quirk"

But how to store the value of Serial.print(floantN,L) in a variable ?

unixbox911:
But how to store the value of Serial.print(floantN,L) in a variable ?

It doesn't have any value to store. It's just printing the value that was already in the variable, so really you already have that value.