Home » Python » python – Best ways to round a number up using string formatting-Exceptionshub

python – Best ways to round a number up using string formatting-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a code like this:

x=5.25
print("%.0f%%"%x)

This prints out: 5%

How can I make it print out 6% instead? So like if x=5.0001 it would still be 6%. Like if it’s above an integer value, round up. How can I do this using string formatting? This rounds my answer, but it rounds it down instead.

I know about round() and math.ceil() but can I do it with string formating?

How to&Answers: