0

What is the synonym of JS toFixed(2) method in Java Language. Only option is DecimalFormat ?

4

1 回答 1

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

虽然这不圆

于 2017-02-11T18:22:40.427 回答