commit f88a971e4ff211b78695609b4482fb886f30a1af (tree)
parent 242ab81112c05fa815523551a6f612c5a12c52b2
Author: Leo Emar-Kar <46078689+emar-kar@users.noreply.github.com>
Date: Sat, 16 Mar 2024 23:38:46 +0000
std.fmt: fix incorrect rounding on 0 precision of a decimal
Diffstat:
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/lib/std/fmt/ryu128.zig b/lib/std/fmt/ryu128.zig
@@ -2,6 +2,7 @@
//! https://dl.acm.org/doi/pdf/10.1145/3360595
const std = @import("std");
+const expectFmt = std.testing.expectFmt;
const special_exponent = 0x7fffffff;
@@ -131,7 +132,7 @@ fn round(f: FloatDecimal128, mode: RoundMode, precision: usize) FloatDecimal128
switch (mode) {
.decimal => {
- if (f.exponent >= 0) {
+ if (f.exponent > 0) {
round_digit = (olength - 1) + precision + @as(usize, @intCast(f.exponent));
} else {
const min_exp_required = @as(usize, @intCast(-f.exponent));
@@ -1129,3 +1130,10 @@ test "format f128" {
try check(f128, 9.409340012568248e18, "9.409340012568248e18");
try check(f128, 1.2345678, "1.2345678e0");
}
+
+test "format float to decimal with zero precision" {
+ try expectFmt("5", "{d:.0}", .{5});
+ try expectFmt("6", "{d:.0}", .{6});
+ try expectFmt("7", "{d:.0}", .{7});
+ try expectFmt("8", "{d:.0}", .{8});
+}