Android - Round to 2 decimal places [duplicate]

2019-01-16 08:42发布

Possible Duplicate:
Round a double to 2 significant figures after decimal point

I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a String and ALWAYS have 2 decimal places?

2条回答
Anthone
2楼-- · 2019-01-16 09:19

One easy way to do it:

    Double d;
    Int i;
    D+=0.005;
    i=d*100;
    Double b = i/100;
    String s = b.toString():
查看更多
三岁会撩人
3楼-- · 2019-01-16 09:21

You can use String.format("%.2f", d), your double will be rounded automatically.

查看更多
登录 后发表回答