2017-02-11 15 views
0

Java 언어에서 JS toFixed (2) 메소드의 동의어는 무엇입니까? 옵션은 DecimalFormat입니까?Java toFixed (2) 메소드와 같은 js

+1

가능한 중복 (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-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을 사용하기로했습니다. 다른 사람들보다 유용하다. –