Java: Count the number of decimal places in a given number
Java Regular Expression: Exercise-17 with Solution
Write a Java program to count the number of decimal places in a given number.
Sample Solution-1:
Java Code:
public class test {
public static void main(String[] args) {
String n = "123";
System.out.println("Original Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.3";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.03";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.233";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
}
public static Integer validate(String n) {
if(n.contains("."))
return n.replaceAll(".*\\.(?=\\d?)", "").length();
return 0;
}
}
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:
public class test {
public static void main(String[] args) {
String n = "123";
System.out.println("Original Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.3";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.03";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
n = "112.233";
System.out.println("\nOriginal Number: "+n);
System.out.println("Number of decimal places in the said number: "+validate(n));
}
public static Integer validate(String n) {
int ctr = n.indexOf(".");
return ctr > 0 ? n.length() - ctr - 1 : 0;
}
}
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 :
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.
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook