[Solved] String.format does not print leading zero using float specifier

I have ran into an issue where string.format does not print a leading zero when using the float specifier. Here’s the code:

message ..= string.format("Length: %3d:%02d:%03.1f\n", thour, tmin, tsec)

The problem is %03.1f. There is no leading zero so if the value is less than 10, it displays a single digit before the decimal place. A Google search doesn’t reveal anything, and the document on string.format() specifically says that 0 prints a leading zero. It does not have any other qualifiers.

Does anyone have any suggestions?

Thanks.

The issue was solved. The correct way to code this is

message ..= string.format("Length: %3d:%02d:%04.1f\n", thour, tmin, tsec)

Apparently the period is included in the returned character count. Makes sense since the documentation says the number of characters which implies the period is included. I just wish that it was spelled out.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.