decimalLength
Function parameters
Parameters
- v:anytype
Type definitions in this namespace
Types
Returns the minimum buffer size needed to print every float of a specific type and format.
Functions
- bufferSize
- Returns the minimum buffer size needed to print every float of a specific type and format.
- render
- Format a floating-point value and write it to buffer.
- formatScientific
- Write a FloatDecimal to a buffer in scientific form.
- formatDecimal
- Write a FloatDecimal to a buffer in decimal form.
- binaryToDecimal
- Convert a binary float representation to decimal.
Error sets in this namespace
Error Sets
Any buffer used for `format` must be at least this large.
Values
- min_buffer_size
- Any buffer used for `format` must be at least this large.
Source
Implementation
fn decimalLength(v: anytype) u32 {
switch (@TypeOf(v)) {
u32, u64 => {
std.debug.assert(v < 100000000000000000);
if (v >= 10000000000000000) return 17;
if (v >= 1000000000000000) return 16;
if (v >= 100000000000000) return 15;
if (v >= 10000000000000) return 14;
if (v >= 1000000000000) return 13;
if (v >= 100000000000) return 12;
if (v >= 10000000000) return 11;
if (v >= 1000000000) return 10;
if (v >= 100000000) return 9;
if (v >= 10000000) return 8;
if (v >= 1000000) return 7;
if (v >= 100000) return 6;
if (v >= 10000) return 5;
if (v >= 1000) return 4;
if (v >= 100) return 3;
if (v >= 10) return 2;
return 1;
},
u128 => {
const LARGEST_POW10 = (@as(u128, 5421010862427522170) << 64) | 687399551400673280;
var p10 = LARGEST_POW10;
var i: u32 = 39;
while (i > 0) : (i -= 1) {
if (v >= p10) return i;
p10 /= 10;
}
return 1;
},
else => unreachable,
}
}