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-a-output-of-float-data-with-2-decimal-places-in-java –
Да, я использовал первый –