how to get sum between two decimal values in android studio











up vote
-2
down vote

favorite












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);









share|improve this question
























  • the system is getting an error -> where is the stacktrace ?
    – 2Dee
    Nov 8 at 8:50










  • 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
    – Solahin Sourjah
    Nov 8 at 8:53










  • you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
    – Vladyslav Matviienko
    Nov 8 at 9:04










  • NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
    – 2Dee
    Nov 8 at 9:10















up vote
-2
down vote

favorite












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);









share|improve this question
























  • the system is getting an error -> where is the stacktrace ?
    – 2Dee
    Nov 8 at 8:50










  • 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
    – Solahin Sourjah
    Nov 8 at 8:53










  • you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
    – Vladyslav Matviienko
    Nov 8 at 9:04










  • NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
    – 2Dee
    Nov 8 at 9:10













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











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);









share|improve this question















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);






android android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 9:12









AS Mackay

1,6213816




1,6213816










asked Nov 8 at 8:48









Solahin Sourjah

32




32












  • the system is getting an error -> where is the stacktrace ?
    – 2Dee
    Nov 8 at 8:50










  • 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
    – Solahin Sourjah
    Nov 8 at 8:53










  • you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
    – Vladyslav Matviienko
    Nov 8 at 9:04










  • NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
    – 2Dee
    Nov 8 at 9:10


















  • the system is getting an error -> where is the stacktrace ?
    – 2Dee
    Nov 8 at 8:50










  • 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
    – Solahin Sourjah
    Nov 8 at 8:53










  • you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
    – Vladyslav Matviienko
    Nov 8 at 9:04










  • NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
    – 2Dee
    Nov 8 at 9:10
















the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50




the system is getting an error -> where is the stacktrace ?
– 2Dee
Nov 8 at 8:50












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
– Solahin Sourjah
Nov 8 at 8:53




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
– Solahin Sourjah
Nov 8 at 8:53












you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
– Vladyslav Matviienko
Nov 8 at 9:04




you obviously can't store decimal values in int variable. As well as it is obvious that 10.5 can't be parsed into int.
– Vladyslav Matviienko
Nov 8 at 9:04












NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10




NumberFormatException: For input string: "2.5". What Vladyslav said. Please Google your exceptions.
– 2Dee
Nov 8 at 9:10












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can also use BigDecimal objects for easy conversion and arithmetics



    BigDecimal decimal1 = new BigDecimal("1.024");
BigDecimal decimal2 = new BigDecimal("52.232");
BigDecimal decimal3 = decimal1.add(decimal2);
BigDecimal decimal4 = decimal3.multiply(decimal1);
String newValue = decimal4.toString();


BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general






share|improve this answer




























    up vote
    1
    down vote













    //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






    share|improve this answer





















    • yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
      – Milos Lulic
      Nov 8 at 9:01










    • Thank you very much. you save my day!
      – Solahin Sourjah
      Nov 8 at 9:08











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204212%2fhow-to-get-sum-between-two-decimal-values-in-android-studio%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You can also use BigDecimal objects for easy conversion and arithmetics



        BigDecimal decimal1 = new BigDecimal("1.024");
    BigDecimal decimal2 = new BigDecimal("52.232");
    BigDecimal decimal3 = decimal1.add(decimal2);
    BigDecimal decimal4 = decimal3.multiply(decimal1);
    String newValue = decimal4.toString();


    BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      You can also use BigDecimal objects for easy conversion and arithmetics



          BigDecimal decimal1 = new BigDecimal("1.024");
      BigDecimal decimal2 = new BigDecimal("52.232");
      BigDecimal decimal3 = decimal1.add(decimal2);
      BigDecimal decimal4 = decimal3.multiply(decimal1);
      String newValue = decimal4.toString();


      BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You can also use BigDecimal objects for easy conversion and arithmetics



            BigDecimal decimal1 = new BigDecimal("1.024");
        BigDecimal decimal2 = new BigDecimal("52.232");
        BigDecimal decimal3 = decimal1.add(decimal2);
        BigDecimal decimal4 = decimal3.multiply(decimal1);
        String newValue = decimal4.toString();


        BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general






        share|improve this answer












        You can also use BigDecimal objects for easy conversion and arithmetics



            BigDecimal decimal1 = new BigDecimal("1.024");
        BigDecimal decimal2 = new BigDecimal("52.232");
        BigDecimal decimal3 = decimal1.add(decimal2);
        BigDecimal decimal4 = decimal3.multiply(decimal1);
        String newValue = decimal4.toString();


        BigDecimal is very powerful especially if you want precision with large decimal places or large numbers in general







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 9:15









        Brandon

        11018




        11018
























            up vote
            1
            down vote













            //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






            share|improve this answer





















            • yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
              – Milos Lulic
              Nov 8 at 9:01










            • Thank you very much. you save my day!
              – Solahin Sourjah
              Nov 8 at 9:08















            up vote
            1
            down vote













            //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






            share|improve this answer





















            • yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
              – Milos Lulic
              Nov 8 at 9:01










            • Thank you very much. you save my day!
              – Solahin Sourjah
              Nov 8 at 9:08













            up vote
            1
            down vote










            up vote
            1
            down vote









            //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






            share|improve this answer












            //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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 8:56









            Bruno

            9261618




            9261618












            • yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
              – Milos Lulic
              Nov 8 at 9:01










            • Thank you very much. you save my day!
              – Solahin Sourjah
              Nov 8 at 9:08


















            • yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
              – Milos Lulic
              Nov 8 at 9:01










            • Thank you very much. you save my day!
              – Solahin Sourjah
              Nov 8 at 9:08
















            yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
            – Milos Lulic
            Nov 8 at 9:01




            yes he have decimals java.lang.NumberFormatException: For input string: "2.5" at java.lang.Integer.parseInt(Integer.java:608)
            – Milos Lulic
            Nov 8 at 9:01












            Thank you very much. you save my day!
            – Solahin Sourjah
            Nov 8 at 9:08




            Thank you very much. you save my day!
            – Solahin Sourjah
            Nov 8 at 9:08


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204212%2fhow-to-get-sum-between-two-decimal-values-in-android-studio%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff