Keep two decimal in double format - Java java.lang

Java examples for java.lang:double Format

Description

Keep two decimal in double format

Demo Code


//package com.java2s;

import java.text.DecimalFormat;

public class Main {

    public static Double decimalTwo(double b) {
        return decimal(b, 2);
    }//from   w ww.  ja v a2 s. co m

    public static Double decimal(double b, int scale) {
        String format = "%1$." + scale + "f";
        DecimalFormat decimal = new DecimalFormat(
                String.format(format, 0.0));
        return new Double(decimal.format(b));
    }
}

Related Tutorials