最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Pythonic way to "round()" like Javascript "Math.round()"? - Stack Overflow

programmeradmin1浏览0评论

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:

  • Math.round() explanation at Mozilla Developers Network (MDN)

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:

  • Math.round() explanation at Mozilla Developers Network (MDN)
Share Improve this question edited Jan 19, 2016 at 12:26 PM 2Ring 55.5k6 gold badges93 silver badges193 bronze badges asked Jan 19, 2016 at 12:15 PaladiniPaladini 4,57215 gold badges57 silver badges97 bronze badges 8
  • 3 Please specify which version of python you are using, as this may be different between python 2 and python 3. – khelwood Commented Jan 19, 2016 at 12:21
  • First of all, why the negative point without any explanation about that? And thank you @khelwood, I'll provide the information. – Paladini Commented Jan 19, 2016 at 12:22
  • 3 This question and its answers might shade light on the situation. – MB-F Commented Jan 19, 2016 at 12:24
  • I've updated the question to include the Python version! – Paladini Commented Jan 19, 2016 at 12:26
  • 1 Thank you for the linked question, @kazemakase . Now I understand why Python3 choose to round numbers this way. – Paladini Commented Jan 19, 2016 at 12:35
 |  Show 3 more comments

3 Answers 3

Reset to default 12
import math
def roundthemnumbers(value):
    x = math.floor(value)
    if (value - x) < .50:
        return x
    else:
        return math.ceil(value)

Haven't had my coffee yet, but that function should do what you need. Maybe with some minor revisions.

The behavior of Python's round function changed between Python 2 and Python 3. But it looks like you want the following, which will work in either version:

math.floor(x + 0.5)

This should produce the behavior you want.

Instead of using round() function in python you can use the floor function and ceil function in python to accomplish your task.

floor(x+0.5)

or

ceil(x-0.5)

发布评论

评论列表(0)

  1. 暂无评论