Challenge: Chop the Decimals
Completed
Start with double price = 499.99;. Print the price, then the price cast to an int, then the text "42" turned into a number plus 8:
499.99 499 50
Input
None.
Output
The three lines above.
Example
Output
499.99 499 50
Editor
Java
Hints
(int) price chops off the decimals.
Integer.parseInt("42") turns the text into the number 42.
Solution explanation
A cast to int drops everything after the decimal point, and Integer.parseInt converts text to a whole number.
Show solution
public class Main {
public static void main(String[] args) {
double price = 499.99;
System.out.println(price);
System.out.println((int) price);
System.out.println(Integer.parseInt("42") + 8);
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module