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 –
是的,我用了第一個 –