Zig 0.17.0-dev (Split by item)
This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig library itself.
Zig › std/ › fmt/ › float.zig › round
round
float.round
fn round (comptime T : type , f : FloatDecimal (T ), mode : RoundMode , precision : usize ) FloatDecimal (T )
File
Code
fn round (comptime T : type , f : FloatDecimal (T ), mode : RoundMode , precision : usize ) FloatDecimal (T ) {
var round_digit : usize = 0 ;
var output = f .mantissa ;
var exp = f .exponent ;
const olength = decimalLength (output );
switch (mode ) {
.decimal => {
if (f .exponent > 0 ) {
round_digit = (olength - 1 ) + precision + @as (usize , @intCast (f .exponent ));
} else {
const min_exp_required = @as (usize , @intCast (-f .exponent ));
if (precision + olength > min_exp_required ) {
round_digit = precision + olength - min_exp_required ;
}
}
},
.scientific => {
round_digit = 1 + precision ;
},
}
if (round_digit < olength ) {
var nlength = olength ;
for (round_digit + 1 ..olength ) |_ | {
output /= 10 ;
exp += 1 ;
nlength -= 1 ;
}
if (output % 10 >= 5 ) {
output /= 10 ;
output += 1 ;
exp += 1 ;
if (isPowerOf10 (output )) {
output /= 10 ;
exp += 1 ;
}
}
}
return .{
.mantissa = output ,
.exponent = exp ,
.sign = f .sign ,
};
}