Search code examples
zig

zig print float precision


In zig it is possible to print float values in decimal notation by using "{d}". This will automatically print the value at full precision. Is there way to specify the number of digits? Either for each value, or as some kind of global setting?


Solution

  • This will limit the number of digits after the decimal point, with rounding and zero-padding:

    format(w, "{d:.1}", .{0.05}) == "0.1"
    format(w, "{d:.3}", .{0.05}) == "0.050"
    

    More info