我需要格式化浮点数到“n”个小数位。格式浮点数到小数点后n位
试图为BigDecimal,但返回的值是不正确的......
public static float Redondear(float pNumero, int pCantidadDecimales) {
// the function is call with the values Redondear(625.3f, 2)
BigDecimal value = new BigDecimal(pNumero);
value = value.setScale(pCantidadDecimales, RoundingMode.HALF_EVEN); // here the value is correct (625.30)
return value.floatValue(); // but here the values is 625.3
}
我需要用我指定的小数号返回一个浮点值。
我需要Float
价值回归不Double
。
请小心,因为String.format取决于您当前的本地配置,您可能无法获得点作为分隔符。首选使用String.format(java.util.Locale.US,“%。2f”,floatValue);' – Gomino 2016-03-02 16:31:09
这个答案给出了一个字符串值,而不是一个浮点值,我不认为是一个好的答案。 – ZhaoGang 2017-03-17 08:21:42
你为什么要强制一个点作为分隔符? – 2017-03-23 10:55:43