Skip to content Skip to sidebar Skip to footer

How To Call Settext() Using A Float?

i tried to call the setText() using the float but it dosnt seem to work can somone help me fix the problem? public class Bmi extends MainActivity { @Override protected voi

Solution 1:

You should convert your float to a String by

r.setText(String.valueOf(result));

Or the quick and dirty way

r.setText("" +result);

If you want it localized (Dot or Comma seperated decimal number)

Stringtext = NumberFormat.getInstance(YOURCONTEXT.getResources().getConfiguration().locale).format(result);
r.setText(text);

Just replace YOURCONTEXT with MainActivity.this if you are in the MainActivity or getActivity() if you are in a Fragment

If you want to set min or max fraction digits try this:

NumberFormat numberformat = NumberFormat.getInstance(YOURCONTEXT.getResources().getConfiguration().locale);
numberformat.setMaximumFractionDigits(2);
numberformat.setMaximumIntegerDigits(1);
numberformat.setMinimumFractionDigits(2);
Stringtext = numberformat.format(result);
r.setText(text);

Solution 2:

You can use

r.setText(String.valueOf(result));

Solution 3:

In order to display float value inside TextView you'll need to convert it to String first.

You can convert any primitive data type(int, float, double,boolean,etc) to String by using String.valueOf(value) method.

Here value is the variable which you want to convert to String.

You can use

Stringstr = String.valueOf(result);
r.setText(str);

Alternatively

r.setText(String.valueOf(result));

Please comment if further help is required.

Post a Comment for "How To Call Settext() Using A Float?"