Skip to content Skip to sidebar Skip to footer

Format Doubles To String

Possible Duplicate: How to nicely format floating types to String? How can I list the number with up to two decimal places? I tried this method: http://developer.android.com/ref

Solution 1:

You can do something like this:

Stringstr = String.format("%.2f", 3.99999);
textView.setText(str);

Solution 2:

Well you can try to manually do it.

//This is just an exampledouble number = result;  //result is YOUR variable (ex. result = 23.1231231241920312)int tmp = number * 100;   //2312.31231241920312
number = (double)tmp / 100;  //23.12

Hope this helps.

Note: You can skip the step where i declare an INT if you do it on the other line.

Update: The advantage of using this method is that you do not need to create an Object which is faster, but of course there are many ways.

Post a Comment for "Format Doubles To String"