Truncates a double value - Android java.lang

Android examples for java.lang:Math

Description

Truncates a double value

Demo Code


//package com.java2s;

public class Main {
    /**//from w w w  . java2s. c o  m
     * Truncates a value
     * @param value - value to be truncated
     * @param places - decimal places
     * @return
     */
    public static double truncate(double value, int places) {
        if (places < 0) {
            throw new IllegalArgumentException();
        }

        long factor = (long) Math.pow(10, places);
        value = value * factor;
        long tmp = (long) value;
        return (double) tmp / factor;
    }
}

Related Tutorials