Monday, January 01, 2018

what does "%.2f" mean? | Codecademy

in my previous  creatinine clearance program
I was getting answers like 64.81481481481482
 so if you want only 1 or  2 values after the decimal point  you can use

what does "%.2f" mean? | Codecademy

https://www.codecademy.com/en/forum_questions/51382b87954cc276830008f9

Dec 31, 2012 - 3 posts - ‎3 authors
% is a symbol for variable and is not a modulo! . and the number following modifies how many digit decimal points you want to print. %f string formatting option treats the value as a decimal, and prints it to six decimal places. the second % outside of the " " sets the variable total to the variable %.2f inside the " "

so now I get a simple answer!

 RESTART: C:/Users/hariharan/AppData/Local/Programs/Python/Python35-32/creatinine clearance2.py 
please enter the Age of the patient in years:60
please enter the Weight of the patient in kg:70
please enter the Serum Creatinine of the patient in mg/dl:1.2
64.81481481481482
>>> 
 RESTART: C:/Users/hariharan/AppData/Local/Programs/Python/Python35-32/creatinine clearance3.py 
please enter the Age of the patient in years:60
please enter the Weight of the patient in kg:70
please enter the Serum Creatinine of the patient in mg/dl:1.2
64.81481481481482
 The Creatinine clearance  is 64.81
>>>


 someone explained it in a most Jargonish way.
if my teacher taught me like this  i would quit  that class on the  second day !"The line ...
print("%.2f" % total)
... uses string interpolation to format the output that is to be printed.
The % between "%.2f" and total serves as a string interpolation operator in this context. The % inside the string "%.2f" signifies that the .2f is part of a format specifier. Since there is one format specifier here, it must be matched by one value to the right of the % operator. In this example, that value is represented by total.2f specifies that the value that it matches should be a of type float, and that its value will be output with 2 places to the right of the decimal point.
So, the statement outputs the value of total with two decimal places"



No comments: