Java Double Number Truncate trunc(double value, int len)

Here you can find the source of trunc(double value, int len)

Description

trunc

License

Apache License

Declaration

public final static double trunc(double value, int len) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public final static double trunc(double value, int len) {

        if (Double.isNaN(value)) {
            return Double.NaN;
        }/*from   ww  w.j a  v a2  s . c o m*/

        if (Double.isInfinite(value)) {
            return value;
        }

        double p = Math.pow(10, len);
        if (value > 0) {
            return (Math.floor(value * p) / p);
        } else if (value < 0) {
            return -(Math.floor(-value * p) / p);
        } else {
            return 0;
        }
    }
}

Related

  1. trunc(double value)
  2. trunc(double value, double threshold)
  3. trunc(double value, int dp)
  4. trunc(double x)
  5. trunc(double x)
  6. trunc(double x, double y)
  7. trunc4(Double number)