Java toFixed(2)方法,如js

Java toFixed(2)方法,如js,java,calculation,tofixed,Java,Calculation,Tofixed,JS toFixed(2)方法在Java语言中的同义词是什么。 唯一的选择是十进制格式 没有,但您可以选择这样做: /** * 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){ /

JS toFixed(2)方法在Java语言中的同义词是什么。
唯一的选择是十进制格式

没有,但您可以选择这样做:

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

这不圆,虽然

可能重复或是的,我使用第一个一串作为返回,我决定使用十进制格式。比别人有用