int(round(x))
จะปัดเศษและเปลี่ยนเป็นจำนวนเต็ม
แก้ไข:
คุณไม่ได้กำหนด int (รอบ (h)) ให้กับตัวแปรใด ๆ เมื่อคุณโทรหา int (รอบ (h)) มันจะคืนค่าจำนวนเต็ม แต่ไม่ทำอะไรเลย คุณต้องเปลี่ยนบรรทัดสำหรับ:
h = int(round(h))
เพื่อกำหนดค่าใหม่ให้กับ h
แก้ไข 2:
ดังที่ @ plowman กล่าวไว้ในข้อคิดเห็นคอมไพ ธ อนround()
ไม่ทำงานอย่างที่คาดหวังและนั่นเป็นเพราะวิธีการจัดเก็บหมายเลขเป็นตัวแปรมักไม่ใช่วิธีที่คุณเห็นบนหน้าจอ มีคำตอบมากมายที่อธิบายพฤติกรรมนี้:
round () ดูเหมือนจะไม่ปัดเศษอย่างเหมาะสม
วิธีหนึ่งในการหลีกเลี่ยงปัญหานี้คือการใช้ทศนิยมตามคำตอบนี้: https://stackoverflow.com/a/15398691/4345659
เพื่อให้คำตอบนี้ทำงานอย่างถูกต้องโดยไม่ต้องใช้ไลบรารีพิเศษมันจะสะดวกในการใช้ฟังก์ชันการปัดเศษที่กำหนดเอง หลังจากการแก้ไขมากมายฉันพบวิธีแก้ไขปัญหาต่อไปนี้ซึ่งเท่าที่ฉันทดสอบได้หลีกเลี่ยงปัญหาการจัดเก็บทั้งหมด มันขึ้นอยู่กับการใช้การเป็นตัวแทนสตริงที่ได้รับด้วยrepr()
(ไม่str()
!) มันดูแฮ็ค แต่มันเป็นวิธีเดียวที่ฉันค้นพบเพื่อแก้ไขทุกกรณี มันทำงานได้กับทั้ง Python2 และ Python3
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
return float(num[:-1])
แบบทดสอบ:
>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>>
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0
ในที่สุดคำตอบที่ถูกต้องจะเป็น:
# Having proper_round defined as previously stated
h = int(proper_round(h))
แก้ไข 3:
แบบทดสอบ:
>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1 # should be 7
gotcha ที่นี่คือที่dec
ทศนิยม -th สามารถเป็น 9 และถ้าdec+1
-th หลัก> = 5 9 จะกลายเป็น 0 และ 1 ควรจะดำเนินการกับตัวเลขdec-1
-th
หากเราพิจารณาสิ่งนี้เราจะได้รับ:
def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
a = num[:-2-(not dec)] # integer part
b = int(num[-2-(not dec)])+1 # decimal part
return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
return float(num[:-1])
ในสถานการณ์ที่อธิบายข้างต้นb = 10
และรุ่นก่อนหน้านี้ก็จะ concatenate a
และb
ซึ่งจะส่งผลในการเรียงต่อกันของ10
ที่ต่อท้าย 0 จะหายไป รุ่นนี้แปลงb
เป็นตำแหน่งทศนิยมที่ถูกต้องตามdec
ความเหมาะสม
int(x)