Java Math Exercises: Get whole and fractional parts from a double value
Java Math Exercises: Exercise-2 with Solution
Write a Java program to get whole and fractional parts from a double value.
Sample Solution:
Java Code:
import java.util.*;
public class Example2 {
public static void main(String[] args) {
double value = 12.56;
double fractional_part = value % 1;
double integral_part = value - fractional_part;
System.out.print("\nOriginal value: "+value);
System.out.print("\nIntegral part: "+integral_part);
System.out.print("\nFractional part: "+fractional_part);
System.out.println();
}
}
Sample Output:
Original value: 12.56 Integral part: 12.0 Fractional part: 0.5600000000000005
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to round up the result of integer division.
Next: Write a Java program to test if a double number is an integer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework