B_old
B_old

Reputation: 1291

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?

Upvotes: 9

Views: 4846

Answers (1)

Dull Bananas
Dull Bananas

Reputation: 1052

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

Upvotes: 16

Related Questions