1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const reduceNumber = extras.reduceNumber;
5
6pub fn fmtByteCountIEC(b: u64) std.fmt.Alt(u64, formatByteCountIEC) {
7 return .{ .data = b };
8}
9
10fn formatByteCountIEC(bytes: u64, writer: *std.Io.Writer) !void {
11 try reduceNumber(writer, bytes, 1024, "B", "KMGTPEZYRQ");
12}
13
14test {
15 try std.testing.expectFmt("1 B", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 0))});
16}
17test {
18 try std.testing.expectFmt("1.000 KB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 1))});
19}
20test {
21 try std.testing.expectFmt("1.000 MB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 2))});
22}
23test {
24 try std.testing.expectFmt("1.000 GB", "{f}", .{fmtByteCountIEC(std.math.pow(u64, 1024, 3))});
25}