You can truncate strings with a printf
field-width specifier:
printf("%.5s", "abcdefgh");
> abcde
Unfortunately it does not work for numbers (replacing d
with x
is the same):
printf("%2d", 1234); // for 34
printf("%.2d", 1234); // for 34
printf("%-2d", 1234); // for 12
printf("%-.2d", 1234); // for 12
> 1234
Is there an easy/trivial way to specify the number of digits to be printed even if it means truncating a number?
MSDN specifically says that it will not happen which seems unnecessarily limiting. (Yes, it can be done by creating strings and such, but I’m hoping for a “printf trick” or clever kludge.)