Uploaded image for project: 'Pig'
  1. Pig
  2. PIG-3926

ROUND_TO function: rounds double/float to fixed number of decimal places

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 0.12.0
    • 0.13.0
    • None
    • Patch Available
    • Hide
      The ROUND_TO(val, digits[, mode]) function accepts a single float or double value, along with an integer number of digits, and returns the value to that number of decimal places. For example, ROUND_TO(3.14159f, 3) returns 3.142f (a float); ROUND_TO(3.14159d, 0) returns 3.0d (a double); and ROUND_TO(1234.56d, -2) returns 1200d. The optional third argument controls the rounding mode; see the documentation.
      Show
      The ROUND_TO(val, digits[, mode]) function accepts a single float or double value, along with an integer number of digits, and returns the value to that number of decimal places. For example, ROUND_TO(3.14159f, 3) returns 3.142f (a float); ROUND_TO(3.14159d, 0) returns 3.0d (a double); and ROUND_TO(1234.56d, -2) returns 1200d. The optional third argument controls the rounding mode; see the documentation.

    Description

      The too-often used trick of rounding to a fixed number of decimal places by writing '1000 * Math.round(num / 1000.0)' is not only unsightly, it's numerically imprecise (http://stackoverflow.com/a/12684082/41857). The attached patch adds a function to do this.

      ROUND_TO(val, digits) accepts a single float or double value, along with an integer number of digits, and returns the value to that number of decimal places

      Examples:

      ROUND_TO(3.14159f, 3) -- returns a float 3.142
      ROUND_TO(3.14159d, 0) -- returns a double 3.0.
      

      I also added unit tests for ROUND – there were none before that I could find.

      I don't know how to write a unit test that the schema correctly routes to FloatRoundTo and DoubleRoundTo – if the ones I provided are insufficient please point me to a simple example to emulate.

      Attachments

        Issue Links

          Activity

            People

              mrflip Flip Kromer
              mrflip Flip Kromer
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: