Python Forum
How to use a variable in Python (2.x) to define decimal part?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a variable in Python (2.x) to define decimal part?
#1
How to use a variable in Python (2.x) to define decimal part?
=============================================================

Thanks for reviewing this thread.

I find the convention of using %.2f to set two decimal places for a float output format. I am looking a way to change the number to a variable so that the number of decimal places displayed based on input/parameter.

Eg:

"%0.2f" % 10.120
'10.12'

pv = 2  #  is variable has the number of decimal places 
"%0.pvf" % 10.120
'10.12'

pv = 3

"%0.pvf" % 10.120
'10.120'
OR
input_v = 445.76528

pv =2 #  is variable has the number of decimal places 
print(format(input_v, '.pvf'))
>> 445.76

pv = 3
print(format(input_v, '.pvf'))
>> 445.765

pv = 4
print(format(input_v, '.pvf'))
>> 445.7652
I find for Python 3.6+ we can do this with f-string style formatter:

num = 0.123456789
precision = 3
print(f"{num:.{precision}f}")
What is the equivalent on Python 2.x (due to security guidelines, only Python 2.x approved for use)?

Thanks for your guidance
Reply
#2
Use format to make a format string.
from math import pi

for i in range(10):
    fmt = '%%0.%df' %i
    print(fmt % pi)
Reply
#3
Remember the format specifier is just a string (like "0.2f"), so you can use any method you might otherwise use to create the string dynamically.

import math

for decimals in range(2,5):
    specifier = "." + str(decimals) + "f"
    print format(math.pi, specifier)
Output:
3.14 3.142 3.1416
Reply
#4
The precision can be passed as an argument in str.format()
# python >= 2.6
rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

nwidth = 1 + max(len(name) for name in rabbits)

for name in sorted(rabbits):
    # the floating point precision is passed as argument to format
    print("{name:{namewidth}}:{score:>10.{precision}f}".format(
          name = name, score = rabbits[name],
          namewidth = nwidth, precision = 2))
Output:
cotton tail : 0.15 flopsy : 0.33 mopsy : 82.29 peter : 8108.11
Reply
#5
(May-06-2021, 07:06 AM)Gribouillis Wrote: The precision can be passed as an argument in str.format()
# python >= 2.6
rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

nwidth = 1 + max(len(name) for name in rabbits)

for name in sorted(rabbits):
    # the floating point precision is passed as argument to format
    print("{name:{namewidth}}:{score:>10.{precision}f}".format(
          name = name, score = rabbits[name],
          namewidth = nwidth, precision = 2))
Output:
cotton tail : 0.15 flopsy : 0.33 mopsy : 82.29 peter : 8108.11

Thanks. I tried. t worked. This is what I was expecting.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 2 395 Jan-09-2024, 08:49 PM
Last Post: jdog
  Calling functions by making part of their name with variable crouzilles 4 748 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Python Struct Question, decimal 10, \n and \x0a. 3python 2 648 Aug-11-2023, 09:29 AM
Last Post: 3python
  Define Variable xinyulon 5 1,195 Nov-03-2022, 01:12 AM
Last Post: Larz60+
  How to write a part of powershell command as a variable? ilknurg 2 1,084 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  How to include input as part of variable name Mark17 4 2,444 Oct-01-2021, 06:45 PM
Last Post: Mark17
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,596 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  define a variable using an if statement Margaridalopes 2 2,147 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 1,989 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Print part of a variable rs74 4 2,524 Jul-27-2020, 04:39 PM
Last Post: rs74

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020