가능한 중복 :이
Round a double to 2 significant figures after decimal point반올림 더블 - 최소 소수 자릿수
아래 코드는
는 35.0로 결과import java.text.DecimalFormat;
public class Test {
public static void main(String[] args){
double x = roundTwoDecimals(35.0000);
System.out.println(x);
}
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.00");
twoDForm.setMinimumFractionDigits(2);
return Double.valueOf(twoDForm.format(d));
}
}
작동합니다. 최소 소수 자릿수를 강제하는 방법은 무엇입니까? 내가 원하는 결과가 35.00
뒤의 0의 수는'double'의 속성이 아니며'String' 표현의 속성입니다. –
http://stackoverflow.com/a/7593617/446885 – Shahzeb
String으로 변환하지 않고 수행하는 방법은 무엇입니까? –