Android - Round to 2 decimal places

AndroidDoubleDecimalRounding

Android Problem Overview


> 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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

One easy way to do it:

    Double d;
    Int i;
    D+=0.005;
    i=d*100;
    Double b = i/100;
    String s = b.toString():

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongoodmView Question on Stackoverflow
Solution 1 - AndroidOleGGView Answer on Stackoverflow
Solution 2 - Androidjersam515View Answer on Stackoverflow