簡體   English   中英

如何在python中四舍五入到10的最高倍數?

[英]How do I round up to the highest multiple of 10 in python?

我有點知道如何使用綜合功能,這就是我目前擁有的(這是整個程序):

#Step 1
print("Enter the first digit of your GTIN code")
digit_1 = int(input())

print("Enter the second digit")
digit_2 = int(input())

print("Enter the third digit")
digit_3 = int(input())

print("Enter the fourth digit")
digit_4 = int(input())

print("Enter the fifth digit")
digit_5 = int(input())

print("Enter the sixth digit")
digit_6 = int(input())

print("Enter the seventh digit")
digit_7 = int(input())

#Step 2
total_1 = digit_1 * 3
total_2 = digit_2 * 1
total_3 = digit_3 * 3
total_4 = digit_4 * 1
total_5 = digit_5 * 3
total_6 = digit_6 * 1
total_7 = digit_7 * 3

#Step 3
final_total = total_1 + total_2 + total_3 + total_4 + total_5 + total_6 + total_7


#Step 4
import math
def roundup(final_total):
    return int(math.ceil(final_total / 10.0)) * 10
final_total_2 = roundup(final_total)


GTIN_number = final_total - final_total_2

print("Your GTIN number is", GTIN_number)

基本上這是我如何計算 GTIN 編號的程序的結尾。該程序沒有計算正確的 GTIN 編號。 例如,數字 3613296 的最后一位數字(第 8 位數字)應為 6,但它的回答為 -6。 我希望你能理解我的意思。 請任何人分解並解釋它,因為我是初學者。

謝謝!

roundup=round(GTINT, -1)
GTIN8 = int(roundup - GTINT) % 10

試一試,桑尼吉姆。

包括后

final_total_2 = roundup(final_total)

你的程序應該可以正常工作。 當您嘗試運行它時發生了什么?

編輯:根據評論,OP 想要的是

GTIN_number = 9-(final_answer-1)%10

或者

GTIN_number = final_answer_2 - final_answer

如果 final_answer_2 是一個四舍五入的 final_answer。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM