0

I have a long list of floating point numbers to be formatted as follows:

Examples:

case 1) 6.0 -> 6.0 (No trailing zeros)

case 2) 1.23456789 -> 1.234567 (or 1.234568) (Max precision of 6)

case 3) 0.000004 -> 0.000004 (No exponent)

I can use

'{}'.format(round(x, 6))

for cases 1 & 2 but 3 gives 4e-06

If I use

'{:6f}'.format(6.0)

I get 6.000000 for case 1)

Is there a clean way to get the formatting I want?

2 Answers2

-1

Use %.6f%(data)

Hope it works !

Jay Parikh
  • 2,419
  • 17
  • 13
-1

Perhaps you can consider this solution.

Your case:

print ('%.6f' % data).rstrip('0').rstrip('.')
Community
  • 1
  • 1
NAmorim
  • 706
  • 6
  • 12