Java: Count the number of decimal places in a given number
Write a Java program to count the number of decimal places in a given number.
Sample Solution-1:
Java Code:
Sample Output:
Original Number: 123 Number of decimal places in the said number: 0 Original Number: 112.3 Number of decimal places in the said number: 1 Original Number: 112.03 Number of decimal places in the said number: 2 Original Number: 112.233 Number of decimal places in the said number: 3
Pictorial Presentation:
Flowchart :
Sample Solution-2:
Java Code:
Sample Output:
Original Number: 123 Number of decimal places in the said number: 0 Original Number: 112.3 Number of decimal places in the said number: 1 Original Number: 112.03 Number of decimal places in the said number: 2 Original Number: 112.233 Number of decimal places in the said number: 3
Flowchart :
For more Practice: Solve these Related Problems:
- Write a Java program to determine the number of digits after the decimal point in a floating-point number represented as a string.
- Write a Java program to count the fractional digits in a decimal number by splitting its string representation.
- Write a Java program to implement a method that returns the number of decimal places in a number, handling cases with no decimal point.
- Write a Java program to compare the decimal precision of two numbers by counting their decimal places.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Replace all the vowels in a given string with a specified character.
Next: Validate a personal identification number (PIN).What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.