commit 558ece8f6f1889bc4773432c16cdf96a54ec1431 (tree)
parent 33c592e981f93f207651b8353a8cbc3d09440353
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Fri, 18 Aug 2017 17:54:42 -0400
slightly nicer floating point printing
Diffstat:
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
@@ -243,12 +243,20 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons
return false;
if (!output(context, "."))
return false;
- if (!output(context, float_decimal.digits[1..]))
- return false;
- if (!output(context, "e"))
- return false;
- if (!formatInt(float_decimal.exp, 10, false, 0, context, output))
- return false;
+ if (float_decimal.digits.len > 1) {
+ if (!output(context, float_decimal.digits[1 .. math.min(usize(7), float_decimal.digits.len)]))
+ return false;
+ } else {
+ if (!output(context, "0"))
+ return false;
+ }
+
+ if (float_decimal.exp != 1) {
+ if (!output(context, "e"))
+ return false;
+ if (!formatInt(float_decimal.exp, 10, false, 0, context, output))
+ return false;
+ }
return true;
}