cfloating-pointavrfloating-point-conversion

Displaying fixed width floating point number before decimal


I want to display floating point number with fixed width before decimal. So far I managed to do it by taking the integer part, displaying desired width and filling with 0 using "%03d" before number value and then displaying the decimal part. I want to know if there is standard way of displaying like for example "%3.3f". I tried the following

printf("value: %3.3f", value);

But the result was only 1 digit before decimal.

How can I achieve it ?


Solution

  • You can use

    printf("%07.3f", value);
    

    where 07 means printing at least 7 characters with padding zero.

    e.g.

    printf("%07.3f", 3.3);

    prints

    003.300