mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 17:12:31 +00:00
Merge pull request #6002 from MasterQ32/zig_fmt_align_fix
Fixes floating point alignment in std.fmt.format
This commit is contained in:
commit
bc176fb739
@ -560,13 +560,25 @@ fn formatFloatValue(
|
||||
options: FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
// this buffer should be enough to display all decimal places of a decimal f64 number.
|
||||
var buf: [512]u8 = undefined;
|
||||
var buf_stream = std.io.fixedBufferStream(&buf);
|
||||
|
||||
if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) {
|
||||
return formatFloatScientific(value, options, writer);
|
||||
formatFloatScientific(value, options, buf_stream.writer()) catch |err| switch (err) {
|
||||
error.NoSpaceLeft => unreachable,
|
||||
else => |e| return e,
|
||||
};
|
||||
} else if (comptime std.mem.eql(u8, fmt, "d")) {
|
||||
return formatFloatDecimal(value, options, writer);
|
||||
formatFloatDecimal(value, options, buf_stream.writer()) catch |err| switch (err) {
|
||||
error.NoSpaceLeft => unreachable,
|
||||
else => |e| return e,
|
||||
};
|
||||
} else {
|
||||
@compileError("Unknown format string: '" ++ fmt ++ "'");
|
||||
}
|
||||
|
||||
return formatBuf(buf_stream.getWritten(), options, writer);
|
||||
}
|
||||
|
||||
pub fn formatText(
|
||||
@ -1791,3 +1803,17 @@ test "padding" {
|
||||
try testFmt("==================Filled", "{:=>24}", .{"Filled"});
|
||||
try testFmt(" Centered ", "{:^24}", .{"Centered"});
|
||||
}
|
||||
|
||||
test "decimal float padding" {
|
||||
var number: f32 = 3.1415;
|
||||
try testFmt("left-pad: **3.141\n", "left-pad: {d:*>7.3}\n", .{number});
|
||||
try testFmt("center-pad: *3.141*\n", "center-pad: {d:*^7.3}\n", .{number});
|
||||
try testFmt("right-pad: 3.141**\n", "right-pad: {d:*<7.3}\n", .{number});
|
||||
}
|
||||
|
||||
test "sci float padding" {
|
||||
var number: f32 = 3.1415;
|
||||
try testFmt("left-pad: **3.141e+00\n", "left-pad: {e:*>11.3}\n", .{number});
|
||||
try testFmt("center-pad: *3.141e+00*\n", "center-pad: {e:*^11.3}\n", .{number});
|
||||
try testFmt("right-pad: 3.141e+00**\n", "right-pad: {e:*<11.3}\n", .{number});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user