简体   繁体   中英

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. For example the number 3613296 should give the last digit (8th digit) as 6, however, it answers as -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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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