I'm trying to get some formatting done in my sprintf statement, but it doesn't seem to work as I expect. Here is the line:
n = sprintf(buffer, "x = %d.%d, y = %d.%d, z = %d.%d \n", x1, x2, y1, y2, z1, z2);
In that printout x1 is the whole part of the number, and x2 is the fractional part. All would be well, except I need to pad x2, y2, and z2 to always be 2 digits - meaning I need to pad with leading zeros.
With examples that I see online it seems like doing this should work:
n = sprintf(buffer, "x = %d.%02d, y = %d.%02d, z = %d.%02d \n", x1, x2, y1, y2, z1, z2);
However, that instead produces something like this:
x = 2.2d, y = 37.2d, z = 2.2d
The 37 above is actually x2, and it apparently got shifted over in place of y1. I tried putting brackets around the '02', but that doesn't do anything either.
I have tried splitting up the period too like this: (but that didn't work)
n = sprintf(buffer, "x = %d. %02d, y = %d. %02d, z = %d. %02d \n", x1, x2, y1, y2, z1, z2);
I'm not really sure what's wrong... I'd appreciate any help. This isn't particularly vital to do in sprintf (I could theoretically write some 'if' statements and get it working that way), but it'd be nice.
Thanks!