Android - Float - Always 00 after point [duplicate]












0
















This question already has an answer here:




  • Java integer-double division confusion [duplicate]

    5 answers




I want to display the average number in the textview.
however, the output is always the integer.
For example, my input num1 is 2 and 3. the output is 2.00.
input: 2 and 7, the output is 4.00.



here is my code.



    <string name="ave">Average: %1$.02f</string> //in strings.xml        

//in main function
int num1 = Integer.parseInt(number1.getText().toString());
int num2 = Integer.parseInt(number2.getText().toString());

//get the average number
float average = ( num1 + num2 ) / 2;
ave_num.setText(getString(R.string.ave, average));









share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 28 '18 at 9:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Java integer-double division confusion [duplicate]

      5 answers




    I want to display the average number in the textview.
    however, the output is always the integer.
    For example, my input num1 is 2 and 3. the output is 2.00.
    input: 2 and 7, the output is 4.00.



    here is my code.



        <string name="ave">Average: %1$.02f</string> //in strings.xml        

    //in main function
    int num1 = Integer.parseInt(number1.getText().toString());
    int num2 = Integer.parseInt(number2.getText().toString());

    //get the average number
    float average = ( num1 + num2 ) / 2;
    ave_num.setText(getString(R.string.ave, average));









    share|improve this question













    marked as duplicate by Mark Rotteveel java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 28 '18 at 9:18


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Java integer-double division confusion [duplicate]

        5 answers




      I want to display the average number in the textview.
      however, the output is always the integer.
      For example, my input num1 is 2 and 3. the output is 2.00.
      input: 2 and 7, the output is 4.00.



      here is my code.



          <string name="ave">Average: %1$.02f</string> //in strings.xml        

      //in main function
      int num1 = Integer.parseInt(number1.getText().toString());
      int num2 = Integer.parseInt(number2.getText().toString());

      //get the average number
      float average = ( num1 + num2 ) / 2;
      ave_num.setText(getString(R.string.ave, average));









      share|improve this question















      This question already has an answer here:




      • Java integer-double division confusion [duplicate]

        5 answers




      I want to display the average number in the textview.
      however, the output is always the integer.
      For example, my input num1 is 2 and 3. the output is 2.00.
      input: 2 and 7, the output is 4.00.



      here is my code.



          <string name="ave">Average: %1$.02f</string> //in strings.xml        

      //in main function
      int num1 = Integer.parseInt(number1.getText().toString());
      int num2 = Integer.parseInt(number2.getText().toString());

      //get the average number
      float average = ( num1 + num2 ) / 2;
      ave_num.setText(getString(R.string.ave, average));




      This question already has an answer here:




      • Java integer-double division confusion [duplicate]

        5 answers








      java android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 12:07









      田睿霖田睿霖

      102




      102




      marked as duplicate by Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 28 '18 at 9:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 28 '18 at 9:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          4 Answers
          4






          active

          oldest

          votes


















          1














          The right side of this assignment:



          float average = (num1 + num2) / 2;


          consists only of integers.

          So after the addition, the division will give the result of integer division.

          This is how the compiler treats mathematical operations between integers.

          Of course you want the result as float.

          So do this:



          float average = 1.0f * ( num1 + num2 ) / 2;


          By multiplying with 1.0f (the f suffix denotes float) you will get the desired float result.

          Or:



          float average = ( num1 + num2 ) / 2f;





          share|improve this answer


























          • 1.0f* is work! thanks a lot!

            – 田睿霖
            Nov 27 '18 at 2:07



















          0














          Use DecimalFormat



            float average = ( num1 + num2 ) / 2;
          DecimalFormat df = new DecimalFormat();
          df.setMaximumFractionDigits(2);

          System.out.println(df.format(average ));


          or String.format



            String average=String.format("%.02f", average )





          share|improve this answer
























          • That won't help because the real problem is integer division, not the formatting

            – Mark Rotteveel
            Nov 28 '18 at 9:19



















          0














          I've already tested this code:



          float average = ( num1 + num2 ) / 2f;





          share|improve this answer

































            -1














            double roundTwoDecimals(double d)
            {
            DecimalFormat twoDForm = new DecimalFormat("#.##");
            return Double.valueOf(twoDForm.format(d));
            }


            or



            String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03

            String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003

            String.format("Value of a: %02d", a) // for 2 didgit ex. 01





            share|improve this answer
























            • That won't help because the real problem is integer division, not the formatting

              – Mark Rotteveel
              Nov 28 '18 at 9:20


















            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The right side of this assignment:



            float average = (num1 + num2) / 2;


            consists only of integers.

            So after the addition, the division will give the result of integer division.

            This is how the compiler treats mathematical operations between integers.

            Of course you want the result as float.

            So do this:



            float average = 1.0f * ( num1 + num2 ) / 2;


            By multiplying with 1.0f (the f suffix denotes float) you will get the desired float result.

            Or:



            float average = ( num1 + num2 ) / 2f;





            share|improve this answer


























            • 1.0f* is work! thanks a lot!

              – 田睿霖
              Nov 27 '18 at 2:07
















            1














            The right side of this assignment:



            float average = (num1 + num2) / 2;


            consists only of integers.

            So after the addition, the division will give the result of integer division.

            This is how the compiler treats mathematical operations between integers.

            Of course you want the result as float.

            So do this:



            float average = 1.0f * ( num1 + num2 ) / 2;


            By multiplying with 1.0f (the f suffix denotes float) you will get the desired float result.

            Or:



            float average = ( num1 + num2 ) / 2f;





            share|improve this answer


























            • 1.0f* is work! thanks a lot!

              – 田睿霖
              Nov 27 '18 at 2:07














            1












            1








            1







            The right side of this assignment:



            float average = (num1 + num2) / 2;


            consists only of integers.

            So after the addition, the division will give the result of integer division.

            This is how the compiler treats mathematical operations between integers.

            Of course you want the result as float.

            So do this:



            float average = 1.0f * ( num1 + num2 ) / 2;


            By multiplying with 1.0f (the f suffix denotes float) you will get the desired float result.

            Or:



            float average = ( num1 + num2 ) / 2f;





            share|improve this answer















            The right side of this assignment:



            float average = (num1 + num2) / 2;


            consists only of integers.

            So after the addition, the division will give the result of integer division.

            This is how the compiler treats mathematical operations between integers.

            Of course you want the result as float.

            So do this:



            float average = 1.0f * ( num1 + num2 ) / 2;


            By multiplying with 1.0f (the f suffix denotes float) you will get the desired float result.

            Or:



            float average = ( num1 + num2 ) / 2f;






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 12:29

























            answered Nov 26 '18 at 12:22









            forpasforpas

            19.3k4829




            19.3k4829













            • 1.0f* is work! thanks a lot!

              – 田睿霖
              Nov 27 '18 at 2:07



















            • 1.0f* is work! thanks a lot!

              – 田睿霖
              Nov 27 '18 at 2:07

















            1.0f* is work! thanks a lot!

            – 田睿霖
            Nov 27 '18 at 2:07





            1.0f* is work! thanks a lot!

            – 田睿霖
            Nov 27 '18 at 2:07













            0














            Use DecimalFormat



              float average = ( num1 + num2 ) / 2;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);

            System.out.println(df.format(average ));


            or String.format



              String average=String.format("%.02f", average )





            share|improve this answer
























            • That won't help because the real problem is integer division, not the formatting

              – Mark Rotteveel
              Nov 28 '18 at 9:19
















            0














            Use DecimalFormat



              float average = ( num1 + num2 ) / 2;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);

            System.out.println(df.format(average ));


            or String.format



              String average=String.format("%.02f", average )





            share|improve this answer
























            • That won't help because the real problem is integer division, not the formatting

              – Mark Rotteveel
              Nov 28 '18 at 9:19














            0












            0








            0







            Use DecimalFormat



              float average = ( num1 + num2 ) / 2;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);

            System.out.println(df.format(average ));


            or String.format



              String average=String.format("%.02f", average )





            share|improve this answer













            Use DecimalFormat



              float average = ( num1 + num2 ) / 2;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);

            System.out.println(df.format(average ));


            or String.format



              String average=String.format("%.02f", average )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 12:10









            sasikumarsasikumar

            7,82511227




            7,82511227













            • That won't help because the real problem is integer division, not the formatting

              – Mark Rotteveel
              Nov 28 '18 at 9:19



















            • That won't help because the real problem is integer division, not the formatting

              – Mark Rotteveel
              Nov 28 '18 at 9:19

















            That won't help because the real problem is integer division, not the formatting

            – Mark Rotteveel
            Nov 28 '18 at 9:19





            That won't help because the real problem is integer division, not the formatting

            – Mark Rotteveel
            Nov 28 '18 at 9:19











            0














            I've already tested this code:



            float average = ( num1 + num2 ) / 2f;





            share|improve this answer






























              0














              I've already tested this code:



              float average = ( num1 + num2 ) / 2f;





              share|improve this answer




























                0












                0








                0







                I've already tested this code:



                float average = ( num1 + num2 ) / 2f;





                share|improve this answer















                I've already tested this code:



                float average = ( num1 + num2 ) / 2f;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '18 at 10:12









                Sreeram TP

                3,32431441




                3,32431441










                answered Nov 26 '18 at 13:14









                Hassan KazemyHassan Kazemy

                1




                1























                    -1














                    double roundTwoDecimals(double d)
                    {
                    DecimalFormat twoDForm = new DecimalFormat("#.##");
                    return Double.valueOf(twoDForm.format(d));
                    }


                    or



                    String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03

                    String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003

                    String.format("Value of a: %02d", a) // for 2 didgit ex. 01





                    share|improve this answer
























                    • That won't help because the real problem is integer division, not the formatting

                      – Mark Rotteveel
                      Nov 28 '18 at 9:20
















                    -1














                    double roundTwoDecimals(double d)
                    {
                    DecimalFormat twoDForm = new DecimalFormat("#.##");
                    return Double.valueOf(twoDForm.format(d));
                    }


                    or



                    String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03

                    String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003

                    String.format("Value of a: %02d", a) // for 2 didgit ex. 01





                    share|improve this answer
























                    • That won't help because the real problem is integer division, not the formatting

                      – Mark Rotteveel
                      Nov 28 '18 at 9:20














                    -1












                    -1








                    -1







                    double roundTwoDecimals(double d)
                    {
                    DecimalFormat twoDForm = new DecimalFormat("#.##");
                    return Double.valueOf(twoDForm.format(d));
                    }


                    or



                    String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03

                    String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003

                    String.format("Value of a: %02d", a) // for 2 didgit ex. 01





                    share|improve this answer













                    double roundTwoDecimals(double d)
                    {
                    DecimalFormat twoDForm = new DecimalFormat("#.##");
                    return Double.valueOf(twoDForm.format(d));
                    }


                    or



                    String.format("Value of a: %.2f", a) // for 2 decimal number ex. 1.03

                    String.format("Value of a: %.3f", a) // for 3 decimal number ex. 1.003

                    String.format("Value of a: %02d", a) // for 2 didgit ex. 01






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '18 at 12:10









                    Vinesh ChauhanVinesh Chauhan

                    612120




                    612120













                    • That won't help because the real problem is integer division, not the formatting

                      – Mark Rotteveel
                      Nov 28 '18 at 9:20



















                    • That won't help because the real problem is integer division, not the formatting

                      – Mark Rotteveel
                      Nov 28 '18 at 9:20

















                    That won't help because the real problem is integer division, not the formatting

                    – Mark Rotteveel
                    Nov 28 '18 at 9:20





                    That won't help because the real problem is integer division, not the formatting

                    – Mark Rotteveel
                    Nov 28 '18 at 9:20



                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen