Money Format Half Up
Python
Medium
3 views
Problem Description
Amount is provided as string like 12, 12.3, 12.345. Output it in money format with 2 decimals using normal rounding (half up).
Input Format
One string amount.
Output Format
One string with 2 decimals.
Official Solution
import sys
from decimal import Decimal,ROUND_HALF_UP
s=sys.stdin.read().strip()
if not s: sys.exit(0)
x=Decimal(s)
y=x.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
sys.stdout.write(f'{y:.2f}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!