I tried without great success to post this both on the user forum and on IRC. I post here since I don't think it is big enough for an RFC (maybe it is).
Context: formatting a float (f32 or f64) using the scientific notation for a human-facing output, especially for well aligned columnar data (a common use case, at least in sciences).
I would like to mimick something like e.g. the F(J)
column here or on the screenshot a the end of the post if the link is broken.
Current behaviour:
let v = 1.34e+2_f32;
assert_eq!("+1.34e2", format!("{:+e}", val));
Expected behaviour:
let v = 1.34e+2_f32;
assert_eq!("+1.34e+02", format!("{:+e}", val));
Example: for a minimal table containing a single column and 2 rows (1.34e2 and -1.34e-2):
the current behavior using the format {:+e}
(or {:+.2e}
, ...) is
+1.34e2
-1.34e-2
while the expected behavior would be:
+1.34e+02
-1.34e-02
Remarks:
- to keep the format expression simple, there is no way (to my knowledge) to modify the way the exponent part of the scientific notation is formatted
- the current behavior is to use the smallest possible number of characters
- it is a valid choice for non-tabular data or for ASCII serializations such as CSV, JSON, ...
- (for ASCII serializations not intended to be read by a human, an equivalent to the %g would probably be more compact)
- the expected behavior follows the C choice
- force the sign to be printed, use 3 (4) characters and pad with '0' knowing that the exponent range is [-38, +38] ([-308, +308]) for a f32 (f64).
- this solution is not the most compact, but it may be the best compromise to keep the format syntax complexity low and to allow well aligned columnar data
Wether you agree or not (I probably have an incomplete, biased view), I really would like someone to share is thoughts on the matter.
P.S: thank you for the great job you are doing, I really enjoy Rust