Skip to content

Commit e90583f

Browse files
bensVexu
authored andcommitted
std.fmt.fmtIntSize{Bin,Dec}: Don't add .0... to bytes
These are the fundamental units so they can't have decimal places.
1 parent 640acf8 commit e90583f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

lib/std/fmt.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,11 @@ fn formatSizeImpl(comptime base: comptime_int) type {
911911
else => unreachable,
912912
};
913913

914-
const s = formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
915-
error.BufferTooSmall => unreachable,
914+
const s = switch (magnitude) {
915+
0 => buf[0..formatIntBuf(&buf, value, 10, .lower, .{})],
916+
else => formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
917+
error.BufferTooSmall => unreachable,
918+
},
916919
};
917920

918921
var i: usize = s.len;
@@ -2096,6 +2099,8 @@ test "filesize" {
20962099
try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeBin(42)});
20972100
try expectFmt("file size: 63MB\n", "file size: {}\n", .{fmtIntSizeDec(63 * 1000 * 1000)});
20982101
try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});
2102+
try expectFmt("file size: 42B\n", "file size: {:.2}\n", .{fmtIntSizeDec(42)});
2103+
try expectFmt("file size: 42B\n", "file size: {:>9.2}\n", .{fmtIntSizeDec(42)});
20992104
try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
21002105
try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});
21012106
try expectFmt("file size: =66.06MB=\n", "file size: {:=^9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});

0 commit comments

Comments
 (0)