Here you can find the source of trim2Decimals(Double value)
public static Double trim2Decimals(Double value)
//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; } }