count Decimal Places in double - Java java.lang

Java examples for java.lang:double

Description

count Decimal Places in double

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double value = 2.45678;
        System.out.println(countDecimalPlaces(value));
    }//from   w  w w .  j  a  va2s.co m

    public static int countDecimalPlaces(double value) {
        if (Math.round(value) == value) {
            return 0;
        }
        final String s = Double.toString(value);
        final int index = s.indexOf('.');
        if (index < 0) {
            return 0;
        }
        return s.length() - 1 - index;
    }
}

Related Tutorials