简体   繁体   中英

Invalid float after new Decimal Format

User want to raise temperature from EditText "set_temp". Currtemp is global string means current temperature

float temp = Float.parseFloat(set_temp.getText().toString());
temp-=0.1;
currTemp=new DecimalFormat("#0.0").format(temp);
set_temp.setText(currTemp);

After this i have an error in logcat

FATAL EXCEPTION: main
Process: ru.unn.caminlab, PID: 7835
java.lang.NumberFormatException: Invalid float: "49,9"

// but why?

It looks like you are parsing a text field, that contains an invalid decimal format.

 float temp = Float.parseFloat(set_temp.getText().toString());

your set_temp field contains "49,9" when it should be "49.9"

I assume they can type into the field. You need to restrict it to a number only field. Not plain text. (to rid , input)

Add this to your xml for the field:

android:inputType="numberDecimal"

1) Check if user having US English language locale, in many cases, user may include float literals language specific.

2) Remove set_temp.getText().toString() from Float.parseFloat() and check set_temp.getText().toString() is returning correct number string literal and then pass it to parseInt().

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