Problems with rounding numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdINo
    New Member
    • Oct 2006
    • 11

    Problems with rounding numbers

    I'm having problems with rounding numbers
    pls help me out
    this is the program instruction that i wrote

    x = 420.30 * 0.15
    y = 420.30 - x

    i print the y out in .2lf format

    and it came out as 357.26

    it needed it to came out 357.25

    i believe that there is a problem with x ( 63.045 ) which is needed to be 63.05
    is there a way i could round x ( 63.045 ) before it is minus with 420.30.

    Thank you in advance
  • AdINo
    New Member
    • Oct 2006
    • 11

    #2
    Problems with rounding numbers

    I'm having problems with rounding numbers
    pls help me out
    this is the program instruction that i wrote

    x = 420.30 * 0.15
    y = 420.30 - x

    i print the y out in .2lf format

    and it came out as 357.26

    it needed it to came out 357.25

    i believe that there is a problem with x ( 63.045 ) which is needed to be 63.05
    is there a way i could round x ( 63.045 ) before it is minus with 420.30.

    Thank you in advance

    Comment

    • AdINo
      New Member
      • Oct 2006
      • 11

      #3
      ooo ya , this is a C programming language

      Comment

      • AdINo
        New Member
        • Oct 2006
        • 11

        #4
        ooo ya , this is a C programming language

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by AdINo
          i believe that there is a problem with x ( 63.045 ) which is needed to be 63.05
          420.30 * 0.15 is 63.045, presumably you have a reason for only wanting to store 2 decimal places?

          You could round it something like

          Code:
          x = ((float)((long)(x*100+0.5)))/100.;
          This will round to 2 decimal places but only if x*100 is in range for a long.

          Comment

          Working...