דור עזרא דור עזרא - 7 months ago 20
Java Question

Put point in string value (android java)

I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:

If the input is

170
, it will become
1.70
.

If the input is
1.70
, it will not change.

This is the code I have:

String weight = editText.getText().toString();

jgm jgm
Answer

Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?

You can convert the String weight to int like this

int wgt = Integer.parseInt(weight); 

Then separating meters and centimetres.

int mtrs = wgt / 100; 
int cms = wgt % 100; 

Then combining both

String result = mtrs + "." + cms;
Comments