0
什么是JS toFixed(2)方法在Java语言中的同义词。 唯一的选择是DecimalFormat?Java toFixed(2)这样的方法js
什么是JS toFixed(2)方法在Java语言中的同义词。 唯一的选择是DecimalFormat?Java toFixed(2)这样的方法js
有没有,但你可以做到这一点作为一种替代方案:
/**
* Shortens a float to a specified length
* @param num The float to shorten
* @param to The length
* @return the shortened version
**/
public static String toFixed(float num, int to){
//Split at the decimal point
String[] s = String.valueOf(num).split("[.]");
//Combine the two so and shorten the second
String ending = s[1].substring(0, ((s[1].length() > to) ? to : s[1].length()));
return s[0] + '.' + ending;
}
这不圆圆,
感谢回报,我决定使用DecimalFormat。比其他人有用 –
的可能的复制[?我如何圆一个双在Java两位小数] (http://stackoverflow.com/questions/5710394/how-do-i-round-a-double-to-two-decimal-places-in-java) –
或http://stackoverflow.com/questions/2538787/how-to-display-an-float-data-with-2-decimal-places-in-java –
是的,我用了第一个 –