mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.fmt: fix incorrect rounding on 0 precision of a decimal
This commit is contained in:
parent
242ab81112
commit
f88a971e4f
1 changed files with 9 additions and 1 deletions
|
|
@ -2,6 +2,7 @@
|
|||
//! https://dl.acm.org/doi/pdf/10.1145/3360595
|
||||
|
||||
const std = @import("std");
|
||||
const expectFmt = std.testing.expectFmt;
|
||||
|
||||
const special_exponent = 0x7fffffff;
|
||||
|
||||
|
|
@ -131,7 +132,7 @@ fn round(f: FloatDecimal128, mode: RoundMode, precision: usize) FloatDecimal128
|
|||
|
||||
switch (mode) {
|
||||
.decimal => {
|
||||
if (f.exponent >= 0) {
|
||||
if (f.exponent > 0) {
|
||||
round_digit = (olength - 1) + precision + @as(usize, @intCast(f.exponent));
|
||||
} else {
|
||||
const min_exp_required = @as(usize, @intCast(-f.exponent));
|
||||
|
|
@ -1129,3 +1130,10 @@ test "format f128" {
|
|||
try check(f128, 9.409340012568248e18, "9.409340012568248e18");
|
||||
try check(f128, 1.2345678, "1.2345678e0");
|
||||
}
|
||||
|
||||
test "format float to decimal with zero precision" {
|
||||
try expectFmt("5", "{d:.0}", .{5});
|
||||
try expectFmt("6", "{d:.0}", .{6});
|
||||
try expectFmt("7", "{d:.0}", .{7});
|
||||
try expectFmt("8", "{d:.0}", .{8});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue