Is there any method to avoid Bankers rounding in Python 3? [duplicate] - python

This question already has answers here:
How to properly round-up half float numbers?
(21 answers)
Closed 2 years ago.
For example
round(18.5) gives me 18
round(19.5) gives me 20
I want the output of round(18.5) to be 19

math.floor(x + 0.5) will always round .5 up.

Related

how do I limit my confidence level number to two decimal using python? [duplicate]

This question already has answers here:
Fixed digits after decimal with f-strings
(8 answers)
Closed 2 years ago.
I am writing confidence level using following code into my Image
text1="color is {}".format(f'Class #{i + 1}-{class_names[ind]}-Confidence: {100 * conf}%')
how do I limit my confidence number to two decimal places rather than the 10 or so it gives?
f'{100 * conf:.2f}' has to work

Excess '4' occurs in the end of number when summing negative floats [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 4 years ago.
For example:
In: -0.01 + (-0.02) + (-0.4) + (-0.05)
Out: -0.48000000000000004
Instead of -0.48
Why does it happen so?
And how to avoid such situations?

Difference in divison of -a//b and a//b in python 2.7 [duplicate]

This question already has answers here:
Floor division with negative number
(4 answers)
Closed 6 years ago.
I am currently working on python 2.7.
I observed that if we have : -10//6 the answer is -2 while 10//6 yields 1
Can i know why is there the difference?
// performs the floor operation.
10/6 is 1.666, the floor of which is 1
-10/6 is -1.666, the floor of which is -2

Printing float up to six decimal places [duplicate]

This question already has answers here:
How can I print many significant figures in Python?
(6 answers)
Closed 6 years ago.
I have an issue in printing a float number.
I tried:
a = 2
c = 4
print (str(c/a).format(1.6))
Output:
2.0
Required Output:
2.000000
How can I print up to 6 decimal places?
This can be accomplished by:
print("{:.6f}".format(your value here));

Python 3 - convert mathematical action to integer [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Closed 6 years ago.
I have a string with a formula 5 - 3, and I need to get the result in integer. How could I do that?
use eval function:
eval("5 - 3") # 2
test = "5-3"
print(eval(test))
Gives 2

Categories

Resources