I've just dipped my toe into Zig, but it seems to be lacking the floating point format options which are common in C and C++, namely the "f" and "g" options, so that, for example, (7.0 / 3.0)
will display as 2.33333
by default, instead of 2.3333333333333335e+00
. Is that on purpose and if so, what is the rationale, or simply an oversight?

- 1,692
- 1
- 13
- 19
-
1Just a general communication tip, when writing questions like this: Don't assume that just because you couldn't find something that it's not there. You could've written the question as "How do you format floating point numbers in zig?" and it'd read less inflammatory than what you've written. – Cubic Jul 21 '23 at 07:33
-
I didn't assume that "it wasn't there". Before writing my question, I had found the format specifications that @ad absurdum linked below, and I tested and tried various options. That's why I said "it *seems to be* lacking" rather than "it *is* lacking". Other than the negative connotation of the word "lacking", I don't see what you consider "inflammatory". – Joe Abbate Jul 21 '23 at 12:52
-
You could theoretically read it that way, except you're taking yourself out of context. Please read the last sentence of your question and explain how you can read the question as anything other than "why are these things not there" given that. – Cubic Jul 21 '23 at 12:57
-
Indeed, I do want to know why those formats are not there, but I don't understand why do you view that as "inflammatory"? It's just an inquiry, as in "inquiring minds want to know", not as "how could they have forgotten that?" Based on ad absurdum's answer, I can understand why the "f" format was left out since the "d" appears to cover what the former does. The only rationale that I can surmise for not supporting "g" is that, although it is the C/C++ default, its output may be inconsistent: sometimes you get scientific notations, sometimes you don't. – Joe Abbate Jul 21 '23 at 14:01
1 Answers
Format string parameters are described here.
You can provide width and precision, among other options. To print a decimal number, use d
(this is not just for integers), and to print a floating point number in scientific notation use e
. For example:
const print = @import("std").debug.print;
pub fn main() void {
const x = 2.0 / 3.0;
print("x = {d:6.5}\n", .{ x });
print("x = {e:6.5}\n", .{ x });
}
Program output:
x = 0.66667
x = 6.66667e-01
Regarding "...the floating point format options which are common in C and C++, namely the f
and g
options....": the f
and g
conversion specifiers aren't just common, the C Standard mandates them in the specification of the Standard Library. Many languages have adopted C style conversion specifiers to greater or lesser degree, but this isn't universal.
Zig does provide for formatting floating point values in decimal notation or scientific notation using d
or e
, respectively. These roughly correspond with C's f
and e
conversion specifiers. There is no option in Zig corresponding with C's g
conversion specifier, which determines decimal vs scientific notation based on the specified precision and the size of the exponent if the value were formatted in scientific notation.
Why doesn't Zig have an analogue to g
? Only the core developers could answer that question. Note that Zig is still early in its development (v0.11.0 is just around the corner at the time of this writing), and the Zig Standard Library in particular is considered tentative. It is possible, even likely, that the formatting facilities will be augmented and improved in the future. Note that there are many other formatting facilities described in the C Standard specification of format string semantics that are not currently supported by Zig; to single out g
seems arbitrary.
It is even possible that the core developers feel like the semantics of g
conflicts with core Zig principles, i.e., the rules for choosing decimal vs. scientific notation for g
are not particularly arcane, but most C programmers probably couldn't tell you exactly what they are off the tops of their heads. Zig values clarity and explicitness, and you could make a case that g
conflicts with this value.
I don't know if Zig developers hold this last view. I wouldn't mind at all if some g
-like functionality were added to the Zig formatting string mechanisms. Zig's format string capabilities still feel incomplete to me at this point, so maybe this particular feature will be added with other improvements in the future.

- 19,498
- 5
- 37
- 60
-
Thanks for the example and the clarification that the `d` format is not just for integers, but what I was really interested was why the other two formats were *not* included, particularly if someone is trying to compare the output of a C program that does floating point division to an equivalent one in Zig. – Joe Abbate Jul 21 '23 at 12:43
-
@JoeAbbate -- the why can only be answered by core developers; I have included some commentary – ad absurdum Jul 21 '23 at 15:22
-
Thanks again. BTW, I wasn't trying to single out `g`. It's just that the second (very basic) program that I tried to convert to Zig happened to exercise FP division and to print out the result. I agree that `g` may have been excluded for consistency reasons. I believe I saw a comment in `fmt` about being able to read back an output value. – Joe Abbate Jul 21 '23 at 15:35
-
@JoeAbbate -- _"I believe I saw a comment in `fmt` about being able to read back an output value."_ I see that now; that's interesting. Common Lisp makes the distinction between printing representations intended for humans to read vs representations which can be parsed into objects by providing two basic functions: `princ` for human-readable, and `prin1` for machine-readable. – ad absurdum Jul 21 '23 at 15:51