Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » How to use a variable in Python (2.x) to define decimal part?(Python formatting)
How to use a variable in Python (2.x) to define decimal part? [message #1841188] Thu, 06 May 2021 01:41
RIMD NA is currently offline RIMD NAFriend
Messages: 12
Registered: January 2014
Location: USA
Junior Member
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?

Thanks for your guidance
Previous Topic:Error in installation of testng plugin
Next Topic:Bug: Auto-complete doesn't work for Java in eclipse 2021-03
Goto Forum:
  


Current Time: Fri Jul 23 23:31:00 GMT 2021

Powered by FUDForum. Page generated in 0.03567 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top