Hướng dẫn why does python round(0.5 to 0) - tại sao python lại làm tròn (0,5 thành 0)

Tạo một tài khoản để theo dõi các cộng đồng yêu thích của bạn và bắt đầu tham gia vào các cuộc trò chuyện.

Tham gia Reddit

r/learnpython

Vòng (0,5) = 0

Vòng (1.5) = 2

Vòng (2.5) = 2

Vòng (3.5) = 4

Có phải chỉ có tôi hay điều này có cảm giác như một loại không nhất quán? Tại sao 0,5 vòng đến 0, khi nó làm tròn đến 1?

Chỉnh sửa: Đó là bởi vì Python sử dụng //en.wikipedia.org/w/index.php?title=ieee_754#rounding_rules

EDIT2: Ngạc nhiên điều này đã nhận được nhiều sự chú ý này. Đây là một mã công việc xung quanh ai đó được thực hiện:

def col_round(x): frac = x - math.floor(x) if frac < 0.5: return math.floor(x) return math.ceil(x)

Vòng trôi nổi xuống 0,5 gần nhất trong Python #

#! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding)

Hi vọng điêu nay co ich,

Narnie

Mục lục #

  1. Vòng trôi nổi vào 0,5 gần nhất trong Python
  2. Vòng trôi nổi lên đến 0,5 gần nhất trong Python
  3. Vòng trôi nổi xuống 0,5 gần nhất trong Python

Vòng trôi nổi vào 0,5 gần nhất trong Python #

Để làm tròn một chiếc phao đến 0,5 gần nhất:

  1. Gọi chức năng #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 1 Truyền số đó nhân với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  3. Kết quả của phép tính là số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Copied!

import math # ✅ Round number to nearest 0.5 def round_to_nearest_half_int(num): return round(num * 2) / 2 print(round_to_nearest_half_int(3.1)) # 👉️ 3.0 print(round_to_nearest_half_int(3.7)) # 👉️ 3.5 # -------------------------------------- # ✅ Round number UP to nearest 0.5 def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # 👉️ 3.5 print(round_up_to_nearest_half_int(3.7)) # 👉️ 4.0 # -------------------------------------- # ✅ Round number DOWN to nearest 0.5 def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # 👉️ 3.5 print(round_down_to_nearest_half_int(3.4)) # 👉️ 3.0

Chúng tôi đã sử dụng hàm #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 1 để làm tròn một số đến 0,5 gần nhất.

Khi được chuyển một đối số duy nhất, hàm tròn làm tròn đến số nguyên gần nhất.

Copied!

print(round(7.4)) # 👉️ 7 print(round(7.6)) # 👉️ 8

Dưới đây là một ví dụ từng bước về việc làm tròn một số lên đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Copied!

print(3.1 * 2) # 👉️ 6.2 print(3.7 * 2) # 👉️ 7.4 print(round(3.1 * 2)) # 👉️ 6 print(round(3.7 * 2)) # 👉️ 7 print(round(3.1 * 2) / 2) # 👉️ 3.0 print(round(3.7 * 2) / 2) # 👉️ 3.5

Đây là một quy trình gồm hai bước:

  1. Nhân số với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 và làm tròn kết quả với số nguyên gần nhất.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 để có được số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Vòng trôi nổi lên đến 0,5 gần nhất trong Python #

Để làm tròn nổi lên đến 0,5 gần nhất:

  1. Gọi phương thức

    Copied!

    import math # ✅ Round number to nearest 0.5 def round_to_nearest_half_int(num): return round(num * 2) / 2 print(round_to_nearest_half_int(3.1)) # 👉️ 3.0 print(round_to_nearest_half_int(3.7)) # 👉️ 3.5 # -------------------------------------- # ✅ Round number UP to nearest 0.5 def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # 👉️ 3.5 print(round_up_to_nearest_half_int(3.7)) # 👉️ 4.0 # -------------------------------------- # ✅ Round number DOWN to nearest 0.5 def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # 👉️ 3.5 print(round_down_to_nearest_half_int(3.4)) # 👉️ 3.0 0 Truyền số đó nhân với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  3. Kết quả của phép tính là số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Copied!

import math def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # 👉️ 3.5 print(round_up_to_nearest_half_int(3.7)) # 👉️ 4.0 print(round_up_to_nearest_half_int(16.2)) # 👉️ 16.5

Chúng tôi đã sử dụng hàm #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 1 để làm tròn một số đến 0,5 gần nhất.

Copied!

import math print(math.ceil(3.1)) # 👉️ 4 print(math.ceil(3.9)) # 👉️ 4

Khi được chuyển một đối số duy nhất, hàm tròn làm tròn đến số nguyên gần nhất.

Dưới đây là một ví dụ từng bước về việc làm tròn một số lên đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Copied!

import math print(6.1 * 2) # 👉️ 12.2 print(6.6 * 2) # 👉️ 13.2 print(math.ceil(6.1 * 2)) # 👉️ 13 print(math.ceil(6.6 * 2)) # 👉️ 14 print(math.ceil(6.1 * 2) / 2) # 👉️ 6.5 print(math.ceil(6.6 * 2) / 2) # 👉️ 7.0

Đây là một quy trình gồm hai bước:

  1. Nhân số với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 và làm tròn kết quả với số nguyên gần nhất.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 để có được số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Vòng trôi nổi lên đến 0,5 gần nhất trong Python #

Để làm tròn nổi lên đến 0,5 gần nhất:

  1. Gọi phương thức

    Copied!

    import math # ✅ Round number to nearest 0.5 def round_to_nearest_half_int(num): return round(num * 2) / 2 print(round_to_nearest_half_int(3.1)) # 👉️ 3.0 print(round_to_nearest_half_int(3.7)) # 👉️ 3.5 # -------------------------------------- # ✅ Round number UP to nearest 0.5 def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # 👉️ 3.5 print(round_up_to_nearest_half_int(3.7)) # 👉️ 4.0 # -------------------------------------- # ✅ Round number DOWN to nearest 0.5 def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # 👉️ 3.5 print(round_down_to_nearest_half_int(3.4)) # 👉️ 3.0 0 Truyền số đó nhân với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2.
  3. Kết quả của phép tính là số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

Copied!

import math def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # 👉️ 3.5 print(round_down_to_nearest_half_int(3.4)) # 👉️ 3.0

Chúng tôi đã sử dụng hàm #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 1 để làm tròn một số đến 0,5 gần nhất.

Copied!

import math print(math.floor(3.9)) # 👉️ 3 print(math.floor(3.1)) # 👉️ 3

Khi được chuyển một đối số duy nhất, hàm tròn làm tròn đến số nguyên gần nhất.

Dưới đây là một ví dụ từng bước về việc làm tròn một số lên đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

#! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 0

Đây là một quy trình gồm hai bước:

  1. Nhân số với #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 và làm tròn kết quả với số nguyên gần nhất.
  2. Chia kết quả cho #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 2 để có được số được làm tròn đến #! /usr/bin/env python3 # -*- coding: utf-8 -*- def trueround(number, places=0): ''' trueround(number, places) example: >>> trueround(2.55, 1) == 2.6 True uses standard functions with no import to give "normal" behavior to rounding so that trueround(2.5) == 3, trueround(3.5) == 4, trueround(4.5) == 5, etc. Use with caution, however. This still has the same problem with floating point math. The return object will be type int if places=0 or a float if places=>1. number is the floating point number needed rounding places is the number of decimal places to round to with '0' as the default which will actually return our interger. Otherwise, a floating point will be returned to the given decimal place. Note: Use trueround_precision() if true precision with floats is needed GPL 2.0 copywrite by Narnie Harshoe <> ''' place = 10**(places) rounded = (int(number*place + 0.5if number>=0 else -0.5))/place if rounded == int(rounded): rounded = int(rounded) return rounded def trueround_precision(number, places=0, rounding=None): ''' trueround_precision(number, places, rounding=ROUND_HALF_UP) Uses true precision for floating numbers using the 'decimal' module in python and assumes the module has already been imported before calling this function. The return object is of type Decimal. All rounding options are available from the decimal module including ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP. examples: >>> trueround(2.5, 0) == Decimal('3') True >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2') True number is a floating point number or a string type containing a number on on which to be acted. places is the number of decimal places to round to with '0' as the default. Note: if type float is passed as the first argument to the function, it will first be converted to a str type for correct rounding. GPL 2.0 copywrite by Narnie Harshoe <> ''' from decimal import Decimal as dec from decimal import ROUND_HALF_UP from decimal import ROUND_CEILING from decimal import ROUND_DOWN from decimal import ROUND_FLOOR from decimal import ROUND_HALF_DOWN from decimal import ROUND_HALF_EVEN from decimal import ROUND_UP from decimal import ROUND_05UP if type(number) == type(float()): number = str(number) if rounding == None: rounding = ROUND_HALF_UP place = '1.' for i in range(places): place = ''.join([place, '0']) return dec(number).quantize(dec(place), rounding=rounding) 4 gần nhất.

0,5 làm tròn lên hay xuống Python?

Đối với 0,5, nó làm tròn lên.For = 0,5, hàm vòng () làm tròn số đến số chẵn gần nhất.Vì vậy, 0,5 được làm tròn về 0, và -0,5;33,5 và 34,5 đều được làm tròn đến 34;-33,5 -34,5 đều được làm tròn đến -34, v.v.. For =0.5, the round() function rounds the number off to the nearest even number. So, 0.5 is rounded to zero, and so is -0.5; 33.5 and 34.5 are both rounded off to 34; -33.5 -34.5 are both rounded off to -34, and so on.

Tại sao Python tròn xuống .5 xuống?

Như mọi khi, Stack Overflow đã có câu trả lời: Python Rounds.5 Thỉnh thoảng vì sự làm tròn của Banker, còn được biết đến với cái tên nhiều thông tin hơn nhiều "Vòng một nửa đến thậm chí".Python sẽ làm tròn.5 số đến toàn bộ gần nhất.sometimes because of Banker's Rounding, also known by the much more informative name "Round Half To Even". Python will round . 5 numbers to the nearest even whole.

Làm cách nào để ngăn chặn Python làm tròn?

Nó chỉ đơn giản là luôn hiển thị một số lượng cố định các chữ số quan trọng.Thử nhập toán;P = 3,14;In p;p = toán học.số Pi;In p.always showing a fixed number of significant digits. Try import math; p=3.14; print p; p=math. pi; print p .

Tại sao Python làm tròn kỳ lạ?

Trực tiếp từ tài liệu: hành vi của vòng () cho phao có thể gây ngạc nhiên: ví dụ, vòng (2.675, 2) cho 2,67 thay vì 2,68 dự kiến.Đây không phải là một lỗi: đó là kết quả của thực tế là hầu hết các phân số thập phân không thể được biểu diễn chính xác dưới dạng phao.most decimal fractions can't be represented exactly as a float.

Chủ đề