 # Learn something every day

I have been struggling to print (Serial.print) floating point numbers and format the decimal places. Somewhere along the line, I had seen that you could format the number of decimal places of a floating number by (pseudo code) Serial.print(floating_point_number,2), where 2 sets the decimal places. Not so. Serial.print(floating_point_number,2) produced a binary number and by experiment, Serial.print(floating_point_number,16) produced the hex number. I have never seen a reference to formatting numbers in this way, only ever using BIN or HEX. Two questions. Is there a reference somewhere to using 2 or 16 to format and secondly, does this imply that my floating_point_number isn't in fact a floating point number, if that makes sense.

If you post the code where floating_point_number is declared, we can tell you. Otherwise it's guesswork (I guess it's not a float type).

Not so.

Wrong. If you print a variable that IS a float, the second argument defines the number of decimal places.

If you print a variable that is an int or a long, the second argument defines the base.

Post some code that you think illustrates the problem. Then, you CAN learn something new every day.

I have never seen a reference to formatting numbers in this way, only ever using BIN or HEX.

What value do you think is associated with the name BIN? With the name HEX?

and secondly, does this imply that my floating_point_number isn't in fact a floating point number

Yes, it does.

Cheers Paul and aarg I had gathered that BIN(ary) meant 2 and HEX(adecimal) meant 16. All I was asking was when formatting, is 2 and BIN the same thing and is 16 and HEX the same thing? As a part-time code dabbler, it threw me a bit, but it's obvious now, just hadn't seen it before. I had had a good look around before asking, but found nothing. As for posting the code, it embarrasses even me, but I would agree that it probably isn't a float. Doing the offending calculation in a separate sketch and declaring everything as floats works fine. For what it's worth, I'm using TimeLib.h and "duration" - I'm beginning to wonder if "duration" is a reserved name. I use it in a simple calculation pulsesPerHour = (10/duration)*3600 - both duration and pulsesPerHour were declared as floats globally at the start of the sketch. I've tried float() etc. to no avail, but I will crack it.

Don't be embarrassed. Better to post the code and feel a little of that and come out smarter as to exactly why that number isn't a float.

Yes, somewhere in the core BIN is defined as 2 and HEX is defined as 16. The names mean the same thing as the numbers.

All I was asking was when formatting, is 2 and BIN the same thing and is 16 and HEX the same thing?

You COULD always print BIN to see what the value really is. Yes, 2 is 2 no matter how you express it. And 16 is 16, no matter how you express it.

And 16 is 16, no matter how you express it.

Unless you do`Serial.print (HEX, HEX);` :D

Unless you do

That doesn't change the value of 16. It expresses it in a different (and confusing, since the output of the function doesn't indicate that the value is not in base 10) way.

``````Serial.print("HEX: 0x");
Serial.println(HEX, HEX);
``````

clears up that confusion, AND identifies WHAT is being printed.

AWOL: Unless you do`Serial.print (HEX, HEX);` :D

Naughty, but I like it !