Half Up Rounding

Half Up Rounding

Easy Python Data Types 18 views
Explanation Complexity

Problem Statement

A decimal number x is provided. Round it to nearest integer (0.5 goes away from zero) and output the integer.

Input Format

First line: A decimal number x

Output Format

• Print one integer: x rounded to nearest integer

• If fractional part is 0.5, round away from zero

Example

-2.5
-3

Constraints

• -10^9 ≤ x ≤ 10^9

Concept Explanation

Input:
x = -2.5

Rule: 0.5 goes away from zero.

• For positive numbers:
2.5 → 3

• For negative numbers:
-2.5 → -3

So output is -3.

Step-by-Step Explanation

1.Read decimal number x as float.

2.If x is positive:

• Add 0.5 and convert to integer.

3.If x is negative:

• Subtract 0.5 and convert to integer.

4.Print the integer result.

Concept Explanation

Input:
x = -2.5

Rule: 0.5 goes away from zero.

• For positive numbers:
2.5 → 3

• For negative numbers:
-2.5 → -3

So output is -3.

Step-by-Step Explanation

1.Read decimal number x as float.

2.If x is positive:

• Add 0.5 and convert to integer.

3.If x is negative:

• Subtract 0.5 and convert to integer.

4.Print the integer result.

Input / Output Format

Input Format
First line: A decimal number x
Output Format
• Print one integer: x rounded to nearest integer

• If fractional part is 0.5, round away from zero
Constraints
• -10^9 ≤ x ≤ 10^9

Examples

Input:
-2.5
Output:
-3

Example Solution (Public)

Python
import sysnfrom decimal import Decimal,ROUND_HALF_UPns=sys.stdin.read().strip()nif not s: sys.exit(0)nx=Decimal(s)ny=x.quantize(Decimal('1'), rounding=ROUND_HALF_UP)nsys.stdout.write(str(int(y)))

Official Solution Code

import sysnfrom decimal import Decimal,ROUND_HALF_UPns=sys.stdin.read().strip()nif not s: sys.exit(0)nx=Decimal(s)ny=x.quantize(Decimal('1'), rounding=ROUND_HALF_UP)nsys.stdout.write(str(int(y)))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.