【问题标题】:How to round to 3 decimals? [duplicate]如何四舍五入到小数点后三位? [复制]
【发布时间】:2013-07-23 03:05:01
【问题描述】:

我有

double a= = 13;
double b = 6;
double c = a/b;

我想确保它以小数点后三位返回。我试过了

Math.round(c); //gives me an error instead.

【问题讨论】:

  • 显示什么错误?

标签: android math rounding decimal


【解决方案1】:

如果您需要格式化输出,例如打印或在文本视图中显示,请使用:

DecimalFormat threeDecRound = new DecimalFormat();
threeDecRound.setMaximumFractionDigits(3);
threeDecRound.setRoundingMode(RoundingMode.HALF_DOWN);
threeDecRound.format(number)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多