Write a java program to count the number of digits before and after the decimal point

Write a java program to count the number of digits before and after the decimal point. For a given double number with at least one decimal value, Write a program to compute the number of digits before and after the decimal point in the following format – noOfDigitsBeforeDecimal:noOfDigitsAfterDecimal.

Note: Ignore zeroes at the end of the decimal (Except if zero is the only digit after the decimal. Refer to Sample  Input & Output 2 and 3)

  • Include a class UserMainCode with a static method findNoDigits which accepts the decimal value. The return type is a string.
  • Create a Class Main which would be used to accept the string and call the static method present in UserMainCode.

Input and Output Format:

  • Input consists of a double.
  • The output consists of a string.

Refer sample output for formatting specifications:

Sample Input 1:
843.21

Sample Output 1:
3:2

Sample Input 2:
20.130

Sample Output 2:
2:2

Sample Input 3:
20.130

Sample Output 3:
2:2

Java program to count the number of digits before and after the decimal point

Following are the steps to count the number of digits before and after decimal points:

  • Declare and initialize a decimal number of type double to a variable.
  • Pass it to the noOfDigits() method.
  • Inside the method, initialize a variable n1 and n2 to 0. Convert the input decimal number of type double to a string using the valueOf() method.
  • Now, use a StringTokenizer instance to break the string into tokens by using ‘.’ as a delimiter. Here, you will get two tokens first is the string before the dot(.) and the string after the dot(.).
  • Get both the length of the tokens in n1 and n2.
  • Now, check for the below conditions:
    • If the character at the 0th position in the first token is 0, then decrease the length by 1 and store it in n1.
    • Now, if the length of the second token i.e value of n2 is not equal to 1, then check if the last character is 0, then decrease the length of the second token and store it in n2.
  • Now, print the value in n1 and n2 by concatenation it with ‘:’ and return them.
StringTokenizer

StringTokenizer is a class in Java that breaks the string into tokens. It has three overloaded constructors:

  1. StringTokenizer(String str): Here, we have default delimiters such as newline, space, a carriage return.
  2. StringTokenizer(String str, String delim): Here, we set the delimiter. Like in this example, we have set (:) as a delimiter.
  3. StringTokenizer(String str, String delim, boolean flag): Here, if the boolean value is true delimiter characters are considered as tokens else they are considered separate tokens.

import java.util.*;
public class Main {
public static void main(String[] args) {
double d=845.69;
System.out.println(noOfDigits(d));
}
public static String noOfDigits(double d) {
int n1=0,n2=0;
String s=String.valueOf(d);
StringTokenizer t=new StringTokenizer(s,".");
String s1=t.nextToken();
String s2=t.nextToken();
n1=s1.length();
n2=s2.length();
if(s1.charAt(0)=='0')
n1=s1.length()-1;
if(n2!=1)
if(s2.charAt(s2.length()-1)=='0')
n2=s2.length()-1;
String s3=String.valueOf(n1)+":"+String.valueOf(n2);
return s3;
}
}

Output

Thus, in this way, we learn how to count the number of digits before and after decimal points.