How to convert cents to dollars? Firstly we should know how many cents is how many dollars and cents.
Answer: 1 dollar is equal to 100 cent.
Write a statement that prints the value of price in the form “X dollars and Y cents” on a line by itself.
So, if the value of price was 1002, your code would print “10 dollars and 2 cents”.
If the value was 511 it would print “5 dollars and 11 cents”.
Solution 1:
1 2 3 4 5 6 7 8 |
centsStr=(input('Amount : ')) d, c = centsStr[:-2], centsStr[-2:] if int(centsStr) > 99: print(d + ' dollars and ' + c + ' cents') else: print('0 dollars and ' + c + ' cents') |
Solution 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def formatPrint(cents): centsStr = str(cents) d, c = centsStr[:-2], centsStr[-2:] if cents > 99: print(d + ' dollars and ' + c + ' cents') else: print('0 dollars and ' + c + ' cents') return x=int(input('Amount : ')) formatPrint(x) |
Output: I entered cents value as 1010