Search code examples
javaandroiddigitsplaintext

Android how can i set numbers of a plain text digits after dot


i'm making a calculation of a formula. i wrote the codes and it worked but i want to see result of velocity text like this:

5.28 (only 2 numbers after the dot) Digits Property of plaint text is not worked for me. i stated velocity as double.

how can i do it ?

sorry for my poor English and Thank you.

edit:

    ...
    public void onButtonClick(View v)
    after some formulas
    velocity2 = velocity * 0.00508;
    drop2 = pressuredrop * 249.174;

    vel.setText(Double.toString(velocity2));
    dr.setText(Double.toString(drop2));
    }

now i must put your code into my codes but i don't know where :) I'm sorry for this simple question. I really need to learn a lot of topics about java.


Solution

  • You can use the decimal formatter for this as-

    NumberFormat formatter = new DecimalFormat("#0.00");     
    Log.d("formatted no is ",""+formatter.format(4.0));
    

    Edit You can modify your code like this-

     public void onButtonClick(View v)
        after some formulas
        velocity2 = velocity * 0.00508;
        drop2 = pressuredrop * 249.174;
    
        vel.setText(formatter.format(velocity2));
        dr.setText(formatter.format(drop2));
        }