I'm trying to print floating point numbers 1.2345678, 0.1234567, 123.45678 using printf in such a way that only the first n characters are printed. I want these number to line up. The %g format specifier in printf("%*g", n, var) does this but the specifier is not treating the 0 in 0.1234567 as a significant figure. This causes the alignment of 0.1234567 to go off wrt to the other two figures.
What's the best way to align the numbers in the formats given. Either by treating 0 as significant with %g or using some other method?
By definition leading zeros are not significant figures. If you want the first N digits to be printed, including leading zeros, convert first the numbers to strings and use
printf("%.*s", n, var_as_string).This way, the first
ncharacters are indeed printed, as you asked. Of course the decimal mark., as any other character, is now significant, contrary to%g.