| ... | ... | @@ -560,13 +560,19 @@ fn formatFloatValue( |
| 560 | 560 | options: FormatOptions, |
| 561 | 561 | writer: anytype, |
| 562 | 562 | ) !void { |
| 563 | // this buffer should be enough to display all decimal places of a decimal f64 number. |
| 564 | var buf: [512]u8 = undefined; |
| 565 | var buf_stream = std.io.fixedBufferStream(&buf); |
| 566 | |
| 563 | 567 | if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { |
| 564 | | return formatFloatScientific(value, options, writer); |
| 568 | try formatFloatScientific(value, options, buf_stream.writer()); |
| 565 | 569 | } else if (comptime std.mem.eql(u8, fmt, "d")) { |
| 566 | | return formatFloatDecimal(value, options, writer); |
| 570 | try formatFloatDecimal(value, options, buf_stream.writer()); |
| 567 | 571 | } else { |
| 568 | 572 | @compileError("Unknown format string: '" ++ fmt ++ "'"); |
| 569 | 573 | } |
| 574 | |
| 575 | return formatBuf(buf[0..buf_stream.pos], options, writer); |
| 570 | 576 | } |
| 571 | 577 | |
| 572 | 578 | pub fn formatText( |
| ... | ... | @@ -1791,3 +1797,17 @@ test "padding" { |
| 1791 | 1797 | try testFmt("==================Filled", "{:=>24}", .{"Filled"}); |
| 1792 | 1798 | try testFmt(" Centered ", "{:^24}", .{"Centered"}); |
| 1793 | 1799 | } |
| 1800 | |
| 1801 | test "decimal float padding" { |
| 1802 | var number: f32 = 3.1415; |
| 1803 | try testFmt("left-pad: **3.141\n", "left-pad: {d:*>7.3}\n", .{number}); |
| 1804 | try testFmt("center-pad: *3.141*\n", "center-pad: {d:*^7.3}\n", .{number}); |
| 1805 | try testFmt("right-pad: 3.141**\n", "right-pad: {d:*<7.3}\n", .{number}); |
| 1806 | } |
| 1807 | |
| 1808 | test "sci float padding" { |
| 1809 | var number: f32 = 3.1415; |
| 1810 | try testFmt("left-pad: **3.141e+00\n", "left-pad: {e:*>11.3}\n", .{number}); |
| 1811 | try testFmt("center-pad: *3.141e+00*\n", "center-pad: {e:*^11.3}\n", .{number}); |
| 1812 | try testFmt("right-pad: 3.141e+00**\n", "right-pad: {e:*<11.3}\n", .{number}); |
| 1813 | } |