Java Fraction Format format(double value)

Here you can find the source of format(double value)

Description

format

License

Apache License

Declaration

public static String format(double value) 

Method Source Code


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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

public class Main {

    public static String format(double value) {
        value = round(value);/*from w ww  .  j  a va  2 s.  co m*/
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMinimumFractionDigits(2);
        format.setMaximumFractionDigits(2);
        return format.format(value).replaceAll(",", "");
    }

    public static double round(double value, int scale, RoundingMode roundingMode) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(scale, roundingMode);
        double d = bd.doubleValue();
        bd = null;
        return d;
    }

    public static double round(double value, int scale) {
        return round(value, scale, RoundingMode.HALF_EVEN);
    }

    public static double round(double value) {
        return round(value, 2);
    }
}

Related

  1. format(double price)
  2. format(double val)
  3. format(double val)
  4. format(double val)
  5. format(double value)
  6. format(double value)
  7. format(double value)
  8. format(double value)
  9. format(double value, double unit)