怎样格式化float?
我就像保留小数点后两位。比如输入12。222222显示12。22
输入123。3333显示123。33
怎样格式化float?
float是有精度限制的。我一般用这个方法:import java.math.BigDecimal;
public class FloatFormatTest {
public static void main(String[] args) {
BigDecimal n = new BigDecimal("1234567.56789");
BigDecimal n2 = n.setScale(2, BigDecimal.ROUND_FLOOR);
System.out.println(n);
System.out.println(n2);
}
}
怎样格式化float?
这样是直接截取的,也可以四舍五入,你看一下BigDecimal的文档就知道了。怎样格式化float?
还有一个方法就是把把他×100,转换成int, 然后在/100怎样格式化float?
jdk1.5有更简单的方法,System.out.format("%.02f",10.1234);
熟悉c的同学有福了 熟悉c的同学有福了 同意2楼的说法。。。。
页:
[1]