round double value by decimal places - Android java.lang

Android examples for java.lang:Double

Description

round double value by decimal places

Demo Code

import android.content.Context;
import android.provider.Settings;
import android.telephony.PhoneNumberUtils;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static double round(double value, int places) {
        if (places < 0)
            throw new IllegalArgumentException();

        long factor = (long) Math.pow(10, places);
        value = value * factor;//from ww w . j a v  a2  s .c om
        long tmp = Math.round(value);
        return (double) tmp / factor;
    }

}

Related Tutorials