From 28282dedbd7c67eef300cb962277f052a463c42e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 16 Jul 2017 16:02:41 -0700 Subject: format: support leading zero printing for floats. For a floating-point argument, the following behavior is supported by the ~a, ~s and ~f format directives: if the precision has a leading zero, then leading zeros are added, up to one less than the field width. * stream.c (formatv): Set the precision to one less than the field width when rendering floats, rather than to zero. The output_num code takes the precision as the cue for padding zeros. * txr.1: Updated format documentation, and clarified a few things also. --- stream.c | 4 ++-- txr.1 | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/stream.c b/stream.c index 48738bb7..d5886336 100644 --- a/stream.c +++ b/stream.c @@ -3259,7 +3259,7 @@ val formatv(val stream_in, val fmtstr, struct args *al) vformat_str(stream, lit("#"), width, align, 0); continue; } - precision = 0; + precision = (width ? width - 1 : 0); goto output_num; } case 'd': @@ -3348,7 +3348,7 @@ val formatv(val stream_in, val fmtstr, struct args *al) continue; } - precision = 0; + precision = (width ? width - 1 : 0); goto output_num; default: if (width != 0 || precision_p) { diff --git a/txr.1 b/txr.1 index 22d0b3cb..3dd53160 100644 --- a/txr.1 +++ b/txr.1 @@ -40493,7 +40493,11 @@ or .codn s ) is used. The details are documented in the description of each of these, below. The leading zero flag is -ignored for floating-point values regardless of the conversion specifier. +also taken into account for floating-point values, and treated uniformly by +these directives. If the flag is present, then the printed value's integer +part will be padded with leading zeros up to the width of the field such that +one character of unused space remains in the field, in case a positive or +negative sign needs also to be rendered. For integer or floating-point arguments, if the precision specifier has a .code + @@ -40560,6 +40564,15 @@ Floating point values which are integers are printed without a trailing .code .0 (point zero). +The +.code + +flag in the precision is honored for rendering an explicit +.code + +sign on non-negative values. +If a leading zero is specified in the precision, and a nonzero width is +specified, then the printed value's integer part will be padded with leading +zeros up to one less than the field width. These zeros are placed before the +sign. .coIP s Prints any object in a standard way, as if by the -- cgit v1.2.3