
arduino - issue #549
Way to specify number of digits / places when printing hex, octal, and binary numbers.
Serial.print(n, HEX, 3); // for example
Comment #1
Posted on Jun 25, 2011 by Grumpy RabbitThis needs a lot more specification in order to be implementable.
For instance, when formatting the value 0xff and "3 places", printf can give " ff", "ff ", and "0ff" (formats %3x, % 3x and %.3x respectively).
Similarly, when formatting 0x1234 to 3 places, do you want "1234", "123" or "234"? (incidentally, from printf you can only get 1234, at least as far as my testing went)
With decimal numbers you also have the sign of the number to contend with, increasing the number of things someone could possibly want. printf provides at least 7 functionally different ways to format decimal numbers with 3 places:
%3d : 99: :999: :9999: : -9: :-99: :-999: :-9999: %03d :099: :999: :9999: :-09: :-99: :-999: :-9999: %-3d :99 : :999: :9999: :-9 : :-99: :-999: :-9999: %+3d :+99: :+999: :+9999: : -9: :-99: :-999: :-9999: % 3d : 99: : 999: : 9999: : -9: :-99: :-999: :-9999: %- 3d : 99: : 999: : 9999: :-9 : :-99: :-999: :-9999: %.3d :099: :999: :9999: :-009: :-099: :-999: :-9999:
…then there are even modes you can't specify with just a single number. For instance, "%3.2d" gives " 09", " 99" and "999".
I'm not suggesting that arduino must necessarily be as complex as printf. Simply that more specification is required for this issue to be implementable. Part of that specification would include deciding which one or more of these behaviors (or behaviors inspired by other number-formatting facilities such as C++ iostreams) to actually add to arduino.
Comment #2
Posted on Aug 18, 2011 by Happy RhinoWow, thanks for the comprehensive reference!
This is primarily intended for hex, binary, and octal numbers, and I think the most useful format is: 0ff. Let's not truncate, so write(0x1234, HEX, 3) should give 1234.
Comment #3
Posted on Nov 19, 2011 by Massive GiraffeIs there a reason not to just have a Serial.printf() method that takes C89 syntax format strings?
Comment #4
Posted on Dec 16, 2011 by Happy Rhino(No comment was entered for this change.)
Comment #5
Posted on Jan 2, 2012 by Happy RhinoIssue 767 has been merged into this issue.
Comment #6
Posted on Nov 2, 2012 by Happy GiraffeComment deleted
Status: New
Labels:
Type-Enhancement
Priority-Medium
Component-Core