0 points
Submitted by fahyij
almost 9 years

# what does "%.2f" mean?

As a beginner ,I just want to know the meaning of the code below,or someone kind to paste the link refer to these two statements! thanks in advance;

``````"%.2f"
print("%.2f" % total)``````

“print” treats the % as a special character you need to add, so it can know, that when you type “f”, the number (result) that will be printed will be a floating point type, and the “.2” tells your “print” to print only the first 2 digits after the point.

points
Submitted by Jorge Guardiola
almost 9 years

JBurger over 8 years

Why isn’t this all explained in the course itself - I mean it makes total sense now, also why % between them - usually its showing “percentage of” for example `108 % 100 == 8` doing that gets us the percentage value but you’re saying in this case it adds together?

Sorry I am just starting all this as I switched my major to Comp. Eng. and need a head start/refresher on coding (only used to HTML).

Jorge Guardiola over 8 years

Hi, sry I’ve been away a lot of time from programming languages, but as far as I remember there isn’t any operator in python that gives you the percentage value of a number, if that’s what you want, you could just divide two numbers and multiply by 100. In your example “%” is giving you the reminder of a division without floating point. That’s called modulo. I believe codecademy first makes you type all the code before explaining everything, that’s just one kind of teaching method. I recommend you to buy a book where you can find all the operators used in these programming languages. I knew this about the “%” operator because I have the “C/C++ and java how to program” book. Or you can just search online. And don’t worry, even tho I’m not an expert I’m here to help :)

``%.2f" % total``

% is a symbol for variable and is not a modulo! . and the number fallowing 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 “ “ “%.4f” would print 54.6293 “%.2f” prints 54.63 rounded

points
Submitted by Jonathan