mainframepl-i

pl1 ROUND function for negative precision


In pl1 ROUND(expression1, expression2) function, if expression2 is negative how does rounding happen? For VALC=ROUND(17.126,-1) , what would be the result?


Solution

  • From the IBM documentation for Enterprise PL/I for z/OS, where n corresponds to expresson2 in your question...

    If n is greater than 0, rounding occurs at the (n)th digit to the right of the point. If n is zero or negative, rounding occurs at the (1-n)th digit to the left of the point.

    I recommend becoming familiar with the IBM documentation for their products you use. At the top of the linked page there is a link for "All products" which will take you to a page of, unsurprisingly, all the extant IBM products and links to their documentation.

    Edit in reply to comment seeking clarification...

    I did see this documentation but failed to arrive at the result. In the case of my example, rounding will occur at the 2nd digit to left of decimal point, meaning ? what will be the output and how? it is confusing to me !

    I would expect the result to be 20.

    From the IBM documentation, lightly edited by adding "^" to indicate "to the power of"...

    The value of the result is given by the following formula, where b = 10 if x is DECIMAL: round(x,n) = sign(x)*(b^-n)* floor(abs(x)* (b^n) + 1/2)

    round(17.126,-1) = +1 * 10^-(-1) * floor(abs(17.126) * (10^-1) + 1/2)
    
    = 10 * floor(17.126 * 0.1 + 1/2)
    = 10 * floor(1.7126 + 1/2)
    = 10 * floor(2.2126)
    = 10 * 2
    = 20