0

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/reference/java/text/NumberFormat.html but no luck. Below code. Maybe someone can help me.

package karcio.fuel.economy;


public class FuelEconomy extends Activity 
{
    private EditText editText1;
    private EditText editText2;

private TextView textView4;
private TextView textView6;

private Button button1;

private double miles;
private double liters;
private double result;
private double convertMilesToKm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initParams();
}

private void initParams()
{
    editText1 = (EditText)findViewById(R.id.editText1);
    editText2 = (EditText)findViewById(R.id.editText2);

    textView4 = (TextView)findViewById(R.id.textView4);
    textView6 = (TextView)findViewById(R.id.textView6);

    button1 = (Button)findViewById(R.id.button1);

    button1.setOnClickListener(new Button.OnClickListener() 
    { 
        public void onClick (View v)
        { 
            calculate(); 
        }
    });

}

private void calculate()
{
    miles = Double.parseDouble(editText1.getText().toString());
    liters = Double.parseDouble(editText2.getText().toString());

    convertMilesToKm = miles * 1.61;
    result = 100 * liters / convertMilesToKm;

    textView6.setText(Double.toString(convertMilesToKm));
    textView4.setText(Double.toString(result));         
}

}

Community
  • 1
  • 1
Karol
  • 165
  • 2
  • 6
  • 19
  • http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-types-to-string This is not a specific Android problem. – Nate-Wilkins Aug 30 '12 at 20:52

2 Answers2

4

You can do something like this:

String str = String.format("%.2f", 3.99999);
textView.setText(str);
corgichu
  • 2,580
  • 3
  • 32
  • 46
  • The disadvantage of this is that you need one more object to do that simple task. which in the android guidance says that you need to create the least objects you can. – Jorge Aguilar Aug 30 '12 at 20:56
  • 1
    You would have one String object anyway, and this is one String object. Your method would end up creating that String object when you set the text. – corgichu Aug 30 '12 at 21:00
  • You are right on that, BUT if you test the performance of doing that compared to do it manually and the convert to String you will notice the difference (Inside a big loop of course for just one is the same). – Jorge Aguilar Aug 30 '12 at 21:14
  • Tnx that method works for me. String ConvertMilesToKm = String.format("%.0f", convertMilesToKm); textView6.setText(ConvertMilesToKm); – Karol Aug 30 '12 at 21:15
  • i have to accept that your answer is more helpful for new users than mine, cause i had to explain that the number was not hardcoded so i will give you a upvote – Jorge Aguilar Aug 30 '12 at 21:21
1

Well you can try to manually do it.

//This is just an example
double 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.

Jorge Aguilar
  • 3,442
  • 30
  • 34