Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Round to 2 decimal places [duplicate]

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?

like image 293
goodm Avatar asked Feb 20 '12 18:02

goodm


People also ask

How do you keep a float up to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.


2 Answers

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

like image 54
OleGG Avatar answered Oct 04 '22 21:10

OleGG


One easy way to do it:

    Double d;
    Int i;
    D+=0.005;
    i=d*100;
    Double b = i/100;
    String s = b.toString():
like image 26
jersam515 Avatar answered Oct 04 '22 22:10

jersam515