2017-02-11 6 views
0

Что такое синоним метода JS toFixed (2) на языке Java. Только опция DecimalFormat?Java toFixed (2) метод такой js

+1

Возможный дубликат [? Как округлить двойной до двух знаков после запятой в Java] (http://stackoverflow.com/questions/5710394/how-do-i-round-a-double-to-two-decimal-places-in-java) –

+0

Или http://stackoverflow.com/questions/2538787/how-to-display-a-output-of-float-data-with-2-decimal-places-in-java –

+0

Да, я использовал первый –

ответ

0

Там нет никого, но вы можете сделать это в качестве альтернативы:

/** 
* 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; 
} 

Это не круглая, хотя

+0

Спасибо за возвращение, я решил использовать DecimalFormat. Полезно, чем другие –

 Смежные вопросы

  • Нет связанных вопросов^_^