I want the most Pythonic way to round numbers just like Javascript does (through Math.round()
). They're actually slightly different, but this difference can make huge difference for my application.
Using round()
method from Python 3:
// Returns the value 20
x = round(20.49)
// Returns the value 20
x = round(20.5)
// Returns the value -20
x = round(-20.5)
// Returns the value -21
x = round(-20.51)
Using Math.round()
method from Javascript*:
// Returns the value 20
x = Math.round(20.49);
// Returns the value 21
x = Math.round(20.5);
// Returns the value -20
x = Math.round(-20.5);
// Returns the value -21
x = Math.round(-20.51);
Thank you!
References: