简体   繁体   中英

How to change a double to a string

I have an app which shows a string in an EditText, this string is the result of the operation of two other doubles the user types in two different EditTexts. The problem is that I want the result of the operation to be shown in the third EditText, but for that it has to be a string. Therefore I change the result by the toString method.

The problem starts here, I want the double that will be a string to have only one decimal. For that I used DecimalFormat and created the df format "#.#". And then I changed the text that would be shown in the last EditText to the new double variable with only one decimal (obviously changing it to String).

DecimalFormat df = new DecimalFormat("#.#");
double BMI_trimmed = Double.parseDouble(df.format(BMI));

final EditText BMIResult = (EditText)findViewById(R.id.BMIResult);
BMIResult.setText(Double.toString(BMI_trimmed));

Here I leave you all the code of the myButtonListenerMethod:

public void myButtonListenerMethod(){
    button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final EditText heighText = (EditText)findViewById(R.id.heightInput);
            String heighStr = heighText.getText().toString();
            double height = Double.parseDouble(heighStr);

            final EditText weighText = (EditText)findViewById(R.id.weightInput);
            String weighStr = weighText.getText().toString();
            double weight = Double.parseDouble(weighStr);

            double BMI = (weight)/(height*height);

            DecimalFormat df = new DecimalFormat("#.#");
            double BMI_trimmed = Double.parseDouble(df.format(BMI));

            final EditText BMIResult = (EditText)findViewById(R.id.BMIResult);
            BMIResult.setText(Double.toString(BMI_trimmed));
        }
    });
}

This app runs perfectly on the AVD, I've runned it in three already. But when I run it in a real device and click the button that starts the myButtonListenerMethod, it stops working suddenly and shuts down. The Terminal gives the following error message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bmicalculator, PID: 19058
    java.lang.NumberFormatException: For input string: "24,2"
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)

If anyone knows what the problem is, please tell me I'll try. Honestly I don't understand why it runs in the AVD but it doesn't properly in a real device. Any idea?

You already get the value rounded as you want and as a string from the formatter. Don't try to parse it, just display it.

BMIResult.setText(df.format(BMI));

The problem is probably your phone Locale. Some phones use. as a separator and some use, as separator. Try replacing all "," with "." before parsing from String to Double.

Also try using this answer: https://stackoverflow.com/a/7559011/2249224

double BMI_trimmed = Double.parseDouble(df.format(BMI));
String yourResult = String.valueOf(BMI_trimmed);  

Happy coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM