Created
August 18, 2019 17:10
-
-
Save forstie/031f1d5b2e04dce83063bbea467f53d1 to your computer and use it in GitHub Desktop.
This example shows that QUANTIZE can be used along with decfloat rounding mode to achieve programmer control of rounding numeric values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
values current decfloat rounding mode; | |
set current decfloat rounding mode= round_half_even; | |
stop; | |
-- =============================================================================================================== | |
-- | |
-- QUANTIZE uses the decfloat rounding mode. | |
-- This is the default: ROUND_HALF_EVEN | |
-- | |
-- Round to nearest value; if values are equidistant, round so that the final digit is even. | |
-- If the discarded digits represent greater than half (0.5) of the value of a number in the next left position, | |
-- the result coefficient is incremented by 1 (round up). | |
-- If the discarded digits represent less than half of the value, | |
-- the result coefficient is not adjusted (that is, the discarded digits are ignored). | |
-- Otherwise, the result coefficient is unaltered if its rightmost digit is even, | |
-- or is incremented by 1 (round up) if its rightmost digit is odd (to make an even digit). | |
-- =============================================================================================================== | |
-- | |
-- description: Contrast ROUND vs QUANTIZE | |
-- | |
with rawdata (val) as ( | |
values (1.220), (1.221), (1.222), (1.223), (1.224), (1.225), (1.226), | |
(1.227), (1.228), (1.229), (1.230) | |
) | |
select val, | |
cast(round(val, 2) as numeric(3, 2)) as rounded, | |
cast(quantize(val,.01) as numeric(3, 2)) as quantized | |
from rawdata; | |
stop; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent! I need this one!