Skip to content

Commit

Permalink
std.fmt.fmtIntSize{Bin,Dec}: Don't add .0... to bytes
Browse files Browse the repository at this point in the history
These are the fundamental units so they can't have decimal places.
  • Loading branch information
bens authored and Vexu committed Mar 23, 2024
1 parent 640acf8 commit e90583f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,11 @@ fn formatSizeImpl(comptime base: comptime_int) type {
else => unreachable,
};

const s = formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => unreachable,
const s = switch (magnitude) {
0 => buf[0..formatIntBuf(&buf, value, 10, .lower, .{})],
else => formatFloat(&buf, new_value, .{ .mode = .decimal, .precision = options.precision }) catch |err| switch (err) {
error.BufferTooSmall => unreachable,
},
};

var i: usize = s.len;
Expand Down Expand Up @@ -2096,6 +2099,8 @@ test "filesize" {
try expectFmt("file size: 42B\n", "file size: {}\n", .{fmtIntSizeBin(42)});
try expectFmt("file size: 63MB\n", "file size: {}\n", .{fmtIntSizeDec(63 * 1000 * 1000)});
try expectFmt("file size: 63MiB\n", "file size: {}\n", .{fmtIntSizeBin(63 * 1024 * 1024)});
try expectFmt("file size: 42B\n", "file size: {:.2}\n", .{fmtIntSizeDec(42)});
try expectFmt("file size: 42B\n", "file size: {:>9.2}\n", .{fmtIntSizeDec(42)});
try expectFmt("file size: 66.06MB\n", "file size: {:.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
try expectFmt("file size: 60.08MiB\n", "file size: {:.2}\n", .{fmtIntSizeBin(63 * 1000 * 1000)});
try expectFmt("file size: =66.06MB=\n", "file size: {:=^9.2}\n", .{fmtIntSizeDec(63 * 1024 * 1024)});
Expand Down

0 comments on commit e90583f

Please sign in to comment.