how to get sum between two decimal values in android studio

928 views android
2

// i am working with an app android studio - mrate is the rate for 1 km distance and replacetext is the distance between two points and replacing the (km) form the distance - when distance value has decimal value (10.5 )then the system is getting an error

String mykm =dbDistance.getText().toString();

            String mrate = dbRate.getText().toString();
            System.out.println(mrate);

            String replacetext;
            replacetext = mykm.replace(" km", "");
            System.out.println(replacetext);


            //convert value into int
            int x=Integer.parseInt(mrate);
            int y=Integer.parseInt(replacetext);

            //sum these two numbers
            int z=x+y;

            System.out.println(z);

answered question

the system is getting an error -> where is the stacktrace ?

2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 5 2018-11-08 12:20:19.005 17543-17543/net.atsuae.vehiclebooking I/System.out: 2.5 D/AndroidRuntime: Shutting down VM 2018-11-08 12:20:19.007 17543-17543/net.atsuae.vehiclebooking E/AndroidRuntime: FATAL EXCEPTION: main Process: net.atsuae.vehiclebooking, PID: 17543 java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608) at java.lang.Integer.parseInt(Integer.java:643) at

1 Answer

0

//convert value into int
int x=Integer.parseInt(mrate);
int y=Integer.parseInt(replacetext);

If you've got decimal values, use Double or Float instead of Integer

posted this

Have an answer?

JD

Please login first before posting an answer.