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:error when I lunch the eclipse rcp
Goto Forum:
  


Current Time: Thu Mar 28 14:18:42 GMT 2024

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

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

Back to the top