string_format_modulo.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. print("%s" % 1.0)
  2. print("%r" % 1.0)
  3. print("%d" % 1.0)
  4. print("%i" % 1.0)
  5. print("%u" % 1.0)
  6. # these 3 have different behaviour in Python 3.x versions
  7. # uPy raises a TypeError, following Python 3.5 (earlier versions don't)
  8. #print("%x" % 18.0)
  9. #print("%o" % 18.0)
  10. #print("%X" % 18.0)
  11. print("%e" % 1.23456)
  12. print("%E" % 1.23456)
  13. print("%f" % 1.23456)
  14. print("%F" % 1.23456)
  15. print("%g" % 1.23456)
  16. print("%G" % 1.23456)
  17. print("%06e" % float("inf"))
  18. print("%06e" % float("-inf"))
  19. print("%06e" % float("nan"))
  20. print("%02.3d" % 123) # prec > width
  21. print("%+f %+f" % (1.23, -1.23)) # float sign
  22. print("% f % f" % (1.23, -1.23)) # float space sign
  23. print("%0f" % -1.23) # negative number with 0 padding
  24. # numbers with large negative exponents
  25. print('%f' % 1e-10)
  26. print('%f' % 1e-20)
  27. print('%f' % 1e-50)
  28. print('%f' % 1e-100)
  29. print('%f' % 1e-300)
  30. # large decimal precision should be truncated and not overflow buffer
  31. # the output depends on the FP calculation so only first 2 digits are printed
  32. # (the 'g' with small e are printed using 'f' style, so need to be checked)
  33. print(('%.40f' % 1e-300)[:2])
  34. print(('%.40g' % 1e-1)[:2])
  35. print(('%.40g' % 1e-2)[:2])
  36. print(('%.40g' % 1e-3)[:2])
  37. print(('%.40g' % 1e-4)[:2])
  38. print("%.0g" % 1) # 0 precision 'g'
  39. print('%.1e' % 9.99) # round up with positive exponent
  40. print('%.1e' % 0.999) # round up with negative exponent