Round a double to 3 significant figures

前端 未结 6 1159
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 10:15

Does anybody know how I can round a double value to 3 significant figures like the examples on this website

http://www.purplemath.com/modules/rounding2.htm

6条回答
  •  佛祖请我去吃肉
    2020-12-10 11:21

    double d = ...;
    BigDecimal bd = new BigDecimal(d);
    bd = bd.round(new MathContext(3));
    double rounded = bd.doubleValue();
    

提交回复
热议问题