Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pad leading zeros with a space instead of a zero in C?

Wondering if I did something like

printf("%04.2f", float_variable);

Could I have it print

" 1.15"

Instead of

"01.15"

Weird request I know, but it is what I need. No idea how to start.

like image 397
Zack Avatar asked Feb 15 '23 09:02

Zack


1 Answers

Just drop the 0, which, by definition, means the number will be zero padded:

printf("%5.2f", float_variable);

Notice that the width of the field is five instead of four, since the value is equal to the minimum width of the entire output string, not just the number of digits.

like image 56
Tim Cooper Avatar answered Apr 08 '23 04:04

Tim Cooper