看看,这样可以吗?
package answer5;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* 求黄金分割点,要求四舍五入到小数点后100位。
* @author Administrator
*
*/
public class GoldPoint {
//BigDecimal:不可变的、任意精度的有符号十进制数
BigDecimal result = new BigDecimal(1);
BigDecimal one = new BigDecimal(1);
public static void main(String[] args) {
GoldPoint g = new GoldPoint();
int precision = 0;
for(int i = 0; i < 100; i++){
g.result = g.one.divide(g.result.add(g.one), 100, RoundingMode.HALF_UP);
// g.print();
precision = g.result.precision();
// System.out.println(precision);
}
g.print();
}
public void method() {
result = one.divide(result.add(one));
print();
System.out.println(result.precision());
if(result.precision() < 100)
method();
}
public void print(){
System.out.println(result);
}
} |