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 standard library.
std › fmt › float › round Private Function round Doc Source 137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 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 ;
// e.g. 9999 -> 10000
if (isPowerOf10 (output )) {
output /= 10 ;
exp += 1 ;
}
}
}
return .{
.mantissa = output ,
.exponent = exp ,
.sign = f .sign ,
};
}