003-006-010
Type Casting: Understanding Precision Conversion
Medium
Problem Description
Type Casting: Understanding Precision Conversion
Learning Objective: Understand precision loss when casting from double to int and correctly predict calculation results
Overview
In Java, cast operators are used when converting between different numeric types. When converting from floating-point numbers (double) to integers (int), the decimal part is truncated.
Specifications
Implement the following:
- Initialize a double variable
pricewith 1999.99 - Initialize a double variable
taxRatewith 0.1 - Calculate
price * taxRate - Cast the result to int and store in variable
taxAmount - Output the int-casted
taxAmount
Output Format
199
Hint
- The cast operator
(int)truncates the decimal part - 1999.99 * 0.1 = 199.999, but when cast to int, it becomes 199
Test Cases
※ Output examples follow programming industry standards
Input:
Expected Output:
199
Input:
Expected Output:
199
Input:
Expected Output:
199
Input:
Expected Output:
199
Input:
Expected Output:
199
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 6 free executions remaining
