Java Decimal Format trim2Decimals(Double value)

Here you can find the source of trim2Decimals(Double value)

Description

trim Decimals

License

Open Source License

Declaration

public static Double trim2Decimals(Double value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2018 Arrow Electronics, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License 2.0
 * which accompanies this distribution, and is available at
 * http://apache.org/licenses/LICENSE-2.0
 *
 * Contributors:/*  w  w  w  . ja v  a2 s .  co m*/
 *     Arrow Electronics, Inc.
 *******************************************************************************/

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    public static Double trim2Decimals(Double value) {
        return Double.parseDouble(createDecimalFormatter("#.##").format(value));
    }

    public static Double trim2Decimals(float value) {
        return Double.parseDouble(createDecimalFormatter("#.##").format(value));
    }

    private static DecimalFormat createDecimalFormatter(String pattern) {
        DecimalFormat formatter = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
        formatter.applyPattern(pattern);
        return formatter;
    }
}

Related

  1. toStringForHumans(double doubleToConvert)
  2. toStringNoDigits(double[] v)
  3. toStringScientific(double x)
  4. trim(int degree, double d)
  5. trim(int level, double value)
  6. truncate(double d)