in reply to printf exact field width of floating point number

In a printf statement, the format spec specifies a minimum width - just like in 'C'. If you ask for something to be printed that exceeds that width, it will be printed. I don't often (well almost never use the %g) - the %f format spec takes a width (including the decimal place) and a number of decimal points.

Your question implies that you are willing to lose precision in order to fit within the number of spaces? This is rather bizarre because a fixed field number format like you describe is rare - more common even in 'C' is a space separated number field.

Perl does have the sprintf() function. That may help. Sounds like you want to print a number, if that number cannot "fit", then truncate digits, then if it still can't fit, then use exponential notation. Try sprintf() with %f format, if that is too long, then switch to %g format.

  • Comment on Re: printf exact field width of floating point number