Rounding off a float to two decimal places

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe M Blow

    Rounding off a float to two decimal places

    Hi evreyone

    I just want to kno how to make my value (a float) and round it off to two
    decimal places


    Thanks alot

    Ty


  • Phil...

    #2
    Re: Rounding off a float to two decimal places

    x = (double)int((x+ 0.005)*100.0)/100.0;

    "Joe M Blow" <joemblow@roger s.com> wrote in message
    news:rI%gb.2612 29$Lnr1.5787@ne ws01.bloor.is.n et.cable.rogers .com...[color=blue]
    > Hi evreyone
    >
    > I just want to kno how to make my value (a float) and round it off to two
    > decimal places
    >
    >
    > Thanks alot
    >
    > Ty
    >
    >[/color]


    Comment

    • Joe M Blow

      #3
      Re: Rounding off a float to two decimal places

      I see what you are doing but i was looking for something more like...

      Math.round(vari able);
      that rounds my variable to the nearest whole number...
      i want it only to two decimal places




      Phil... <rynes@ieee.org > wrote in message
      news:YQ%gb.7099 74$Ho3.156706@s ccrnsc03...[color=blue]
      > x = (double)int((x+ 0.005)*100.0)/100.0;
      >
      > "Joe M Blow" <joemblow@roger s.com> wrote in message
      > news:rI%gb.2612 29$Lnr1.5787@ne ws01.bloor.is.n et.cable.rogers .com...[color=green]
      > > Hi evreyone
      > >
      > > I just want to kno how to make my value (a float) and round it off to[/color][/color]
      two[color=blue][color=green]
      > > decimal places
      > >
      > >
      > > Thanks alot
      > >
      > > Ty
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Phil...

        #4
        Re: Rounding off a float to two decimal places

        x = Math.round(x*10 0.0) / 100.0;


        "Joe M Blow" <joemblow@roger s.com> wrote in message
        news:L31hb.2619 54$Lnr1.142716@ news01.bloor.is .net.cable.roge rs.com...[color=blue]
        > I see what you are doing but i was looking for something more like...
        >
        > Math.round(vari able);
        > that rounds my variable to the nearest whole number...
        > i want it only to two decimal places
        >
        >
        >
        >
        > Phil... <rynes@ieee.org > wrote in message
        > news:YQ%gb.7099 74$Ho3.156706@s ccrnsc03...[color=green]
        > > x = (double)int((x+ 0.005)*100.0)/100.0;
        > >
        > > "Joe M Blow" <joemblow@roger s.com> wrote in message
        > > news:rI%gb.2612 29$Lnr1.5787@ne ws01.bloor.is.n et.cable.rogers .com...[color=darkred]
        > > > Hi evreyone
        > > >
        > > > I just want to kno how to make my value (a float) and round it off to[/color][/color]
        > two[color=green][color=darkred]
        > > > decimal places
        > > >
        > > >
        > > > Thanks alot
        > > >
        > > > Ty
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Margaret

          #5
          Re: Rounding off a float to two decimal places

          DecimalFormat df2 = new DecimalFormat( "#,###,###,##0. 00" );
          double dd = 100.2397;
          double dd2dec = new Double(df2.form at(dd)).doubleV alue();

          The value of dd2dec will be 100.24



          "Joe M Blow" <joemblow@roger s.com> wrote in message news:<rI%gb.261 229$Lnr1.5787@n ews01.bloor.is. net.cable.roger s.com>...[color=blue]
          > Hi evreyone
          >
          > I just want to kno how to make my value (a float) and round it off to two
          > decimal places
          >
          >
          > Thanks alot
          >
          > Ty[/color]

          Comment

          • DaiIchi

            #6
            Re: Rounding off a float to two decimal places

            Rita_Shpilsky@h vbamericas.com (Margaret) wrote in message news:<c1c3db4a. 0310090942.f5e1 280@posting.goo gle.com>...[color=blue]
            > DecimalFormat df2 = new DecimalFormat( "#,###,###,##0. 00" );
            > double dd = 100.2397;
            > double dd2dec = new Double(df2.form at(dd)).doubleV alue();
            >
            > The value of dd2dec will be 100.24
            >
            >
            >
            > "Joe M Blow" <joemblow@roger s.com> wrote in message news:<rI%gb.261 229$Lnr1.5787@n ews01.bloor.is. net.cable.roger s.com>...[color=green]
            > > Hi evreyone
            > >
            > > I just want to kno how to make my value (a float) and round it off to two
            > > decimal places
            > >
            > >
            > > Thanks alot
            > >
            > > Ty[/color][/color]



            How about: f = (float) (Math.round(n*1 00.0f)/100.0f);

            Comment

            • John Kordyback

              #7
              Re: Rounding off a float to two decimal places

              double r = 5.1234;
              System.out.prin tln(r); // r is 5.1234

              int decimalPlaces = 2;
              BigDecimal bd = new BigDecimal(r);

              // setScale is immutable
              bd = bd.setScale(dec imalPlaces, BigDecimal.ROUN D_HALF_UP);
              r = bd.doubleValue( );

              System.out.prin tln(r); // r is 5.12

              /qb

              Joe M Blow wrote:[color=blue]
              > Hi evreyone
              >
              > I just want to kno how to make my value (a float) and round it off to two
              > decimal places
              >
              >
              > Thanks alot
              >
              > Ty
              >
              >[/color]

              Comment

              • Virendra Khanzode
                New Member
                • May 2006
                • 1

                #8
                How if i want 123 to be changed to 123.00 to maintain consisten precision for a good view?

                Originally posted by DaiIchi
                Rita_Shpilsky@h vbamericas.com (Margaret) wrote in message news:<c1c3db4a. 0310090942.f5e1 280@posting.goo gle.com>...[color=blue]
                > DecimalFormat df2 = new DecimalFormat( "#,###,###,##0. 00" );
                > double dd = 100.2397;
                > double dd2dec = new Double(df2.form at(dd)).doubleV alue();
                >
                > The value of dd2dec will be 100.24
                >
                >
                >
                > "Joe M Blow" <joemblow@roger s.com> wrote in message news:<rI%gb.261 229$Lnr1.5787@n ews01.bloor.is. net.cable.roger s.com>...[color=green]
                > > Hi evreyone
                > >
                > > I just want to kno how to make my value (a float) and round it off to two
                > > decimal places
                > >
                > >
                > > Thanks alot
                > >
                > > Ty[/color][/color]



                How about: f = (float) (Math.round(n*1 00.0f)/100.0f);

                Comment

                Working...