简体   繁体   English

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

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

I sort of know how to use a roundup function and this is what I currently have (this is the whole program):我有点知道如何使用综合功能,这就是我目前拥有的(这是整个程序):

#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)

Basically this is the end of my program of how to calculate a GTIN number.The program doesn't calculate the correct GTIN number.基本上这是我如何计算 GTIN 编号的程序的结尾。该程序没有计算正确的 GTIN 编号。 For example the number 3613296 should give the last digit (8th digit) as 6, however, it answers as -6.例如,数字 3613296 的最后一位数字(第 8 位数字)应为 6,但它的回答为 -6。 I hope you can understand what I mean.我希望你能理解我的意思。 Please could anybody break it down and explain it as I am a beginner.请任何人分解并解释它,因为我是初学者。

Thank you!谢谢!

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

Give this a whirl, Sonny Jim.试一试,桑尼吉姆。

After including the包括后

final_total_2 = roundup(final_total)

your program should work fine.你的程序应该可以正常工作。 What's happening when you try to run it?当您尝试运行它时发生了什么?

EDIT: Based on comments, what OP wants is编辑:根据评论,OP 想要的是

GTIN_number = 9-(final_answer-1)%10

or或者

GTIN_number = final_answer_2 - final_answer

if final_answer_2 is a rounded up final_answer.如果 final_answer_2 是一个四舍五入的 final_answer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM