In this article, we are going to find digits after decimals in given number. for this, we will write a java code.
Example:-
if n=145.258, then the output will be 258.
Steps:-
- Store number in a string variable.
- found the index of decimal decimal point “.” .
- substring after a decimal point is the desired result i.e. digits after decimal points.
Java code to find digits after decimal numbers:-
package learn;
import java.util.*;
public class DigitafterDecimal {
public static int digitAfterdecimalpoint(String no) {
int pos=no.indexOf('.');
if(pos<0) {
return-1;
}
else {
return pos+1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
System.out.println("Enter the number");
String no= sc.next();
int index=digitAfterdecimalpoint(no);
if(index==-1)
{
System.out.println("no decimal point in the number");
}
else if(index==no.length())
{
System.out.println("digit after decimal is not available ");
}
else {
System.out.println("digits after decimal is: "+ no.substring(index));
}
}
}
Output:-
The same logic can be used to find digits after decimals in other programming languages like C, C++, Python, etc.