Rounding floats/doubles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • itportal

    #1

    Rounding floats/doubles

    Hello,

    This will probably be a simple question. How can I round float/doubles
    to a specific digit after the comma/dot?

    For example we have float i = 1.234567. I want to round it to 1.234.
    Whan function should I use?

  • itportal

    #2
    Re: Rounding floats/doubles


    printf("%.3f ",i);

    rounds the number to 1.235

    How to round it to 1.234.

    Comment

    • Vladimir S. Oka

      #3
      Re: Rounding floats/doubles

      itportal wrote:[color=blue]
      > printf("%.3f ",i);
      >
      > rounds the number to 1.235
      >
      > How to round it to 1.234.
      >[/color]

      One way is to sprintf() with "%f" specifier to a string buffer, and then
      truncate resultant string to taste...

      Cheers

      Vladimir


      --
      My e-mail address is real, and I read it.

      Comment

      • Roberto Waltman

        #4
        Re: Rounding floats/doubles

        <itportal@gmail .com> wrote:[color=blue]
        >For example we have float i = 1.234567. I want
        >to round it to 1.234.[/color]

        and
        [color=blue]
        >printf("%.3f ",i);
        >rounds the number to 1.235[/color]

        "1.235" is what most of us would call
        the correct "rounded" value of "1.234567"

        To "round it down", you could use truncf():

        #include <math.h>
        ...
        #define TWO_DIGITS 100.0
        #define TREE_DIGITS 1000.0
        #define FOUR_DIGITS 10000.0
        ...
        float i, i_truncated;
        i = 1.234567;
        i_truncated = truncf(i * THREE_DIGITS) / THREE_DIGITS;
        ...

        Use trunc(), if you are dealing with double variables.

        I followed your example using 'i', even though it strongly
        suggests an integer data type. (Or it is only for people
        like me, who used FORTRAN long before using C ?)

        Roberto Waltman

        [ Please reply to the group, ]
        [ return address is invalid. ]

        Comment

        • Eric Sosman

          #5
          Re: Rounding floats/doubles

          itportal wrote:
          [color=blue]
          > Hello,
          >
          > This will probably be a simple question. How can I round float/doubles
          > to a specific digit after the comma/dot?
          >
          > For example we have float i = 1.234567. I want to round it to 1.234.
          > Whan function should I use?[/color]

          I can't think of one -- after all, it's a fairly
          unusual "rounding" rule that produces the more distant
          of the two candidate values. However, you could use

          #include <math.h>
          ...
          float j = (float)(floor(i * 1000.0) / 1000.0);

          I don't guarantee this will do what you want with negative
          numbers (you didn't mention what you want). Also, the
          similar version for `double' will probably misbehave with
          starting values larger than DBL_MAX/1000. Finally, note
          that eight significant digits are more than you have any
          right to expect from a `float' ...

          --
          Eric Sosman
          esosman@acm-dot-org.invalid

          Comment

          • Vladimir S. Oka

            #6
            Re: Rounding floats/doubles

            itportal wrote:[color=blue]
            > Hello,
            >
            > This will probably be a simple question. How can I round float/doubles
            > to a specific digit after the comma/dot?
            >
            > For example we have float i = 1.234567. I want to round it to 1.234.
            > Whan function should I use?
            >[/color]

            Oh, BTW, when I learned maths the rule was:

            a.bcdef

            if d<5 round to a.bc
            else if d>5 round to a.b(c+1) and recurse for b
            else if d==5 then
            if c is even round to a.bc
            else ronud to a.b(c+1) and recurse for b

            Cheers

            Vladimir


            --
            My e-mail address is real, and I read it.

            Comment

            • Gordon Burditt

              #7
              Re: Rounding floats/doubles

              >For example we have float i = 1.234567.

              You *DO NOT* have a float of that value on a machine with binary
              floating point.

              If it's not an exact integer and the decimal representation doesn't
              end in 5, there's no exact representation in binary floating point.
              [color=blue]
              >I want to round it to 1.234.[/color]

              You *DO NOT* have a float of that value on a machine with binary
              floating point.
              [color=blue]
              >Whan function should I use?
              >[/color]

              Multiply by 1000, floor() it, then divide by 1000. Dividing by 1000
              re-introduces rounding error.


              Gordon L. Burditt

              Comment

              • Keith Thompson

                #8
                Re: Rounding floats/doubles

                "itportal" <itportal@gmail .com> writes:[color=blue]
                > This will probably be a simple question. How can I round float/doubles
                > to a specific digit after the comma/dot?
                >
                > For example we have float i = 1.234567. I want to round it to 1.234.
                > Whan function should I use?[/color]

                Do want the rounded value to be a floating-point number, or do you
                just want a string to be displayed somewhere?

                If you want a numeric result, keep in mind that neither 1.234567 nor
                1.234 can be represented exactly in binary floating-point. You can
                get a close approximation to 1.234, but it could be either slightly
                larger or slightly smaller than 1.234; if it's smaller, trying to
                truncate it again could give you 1.233.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Lucien Kennedy-Lamb

                  #9
                  Re: Rounding floats/doubles

                  The function below uses an integer cast to remove the fractional part
                  of the value after being pre-scaled. This is an alternative to using
                  the floor() function previously mentioned.

                  double truncate_digits (double d, int significant_dig its)
                  {
                  unsigned long int prescaler;

                  /* Construct the prescaler value for this number of digits */
                  for (prescaler = 1; significant_dig its > 0; significant_dig its--)
                  {
                  prescaler *= 10;
                  }

                  return ((long int)(d * prescaler) / (double)prescal er);
                  }

                  Will this perform the same on all C compilers?

                  Lucien Kennedy-Lamb

                  Comment

                  • Gordon Burditt

                    #10
                    Re: Rounding floats/doubles

                    >The function below uses an integer cast to remove the fractional part[color=blue]
                    >of the value after being pre-scaled. This is an alternative to using
                    >the floor() function previously mentioned.
                    >
                    >double truncate_digits (double d, int significant_dig its)
                    >{
                    > unsigned long int prescaler;
                    >
                    > /* Construct the prescaler value for this number of digits */
                    > for (prescaler = 1; significant_dig its > 0; significant_dig its--)
                    > {
                    > prescaler *= 10;
                    > }
                    >
                    > return ((long int)(d * prescaler) / (double)prescal er);
                    >}
                    >
                    >Will this perform the same on all C compilers?[/color]

                    No. Division by a power of 10 will introduce rounding error on
                    machines using binary floating point (that is: most of them) and
                    how much rounding error (and which direction) depends on the precision
                    of the floating point. This assumes that prescaler > 0 and
                    the exact result with infinite-precision math isn't an integer, which
                    covers just about all of the interesting uses of the function.

                    Oh, yes, even worse things happen if the calculation of prescaler
                    or d*prescaler overflows.

                    Gordon L. Burditt

                    Comment

                    Working...