rdblue commented on code in PR #5030:
URL: https://github.com/apache/iceberg/pull/5030#discussion_r907881410


##########
python/src/iceberg/utils/decimal.py:
##########
@@ -75,3 +75,16 @@ def decimal_to_bytes(value: Decimal) -> bytes:
     """
     unscaled_value = decimal_to_unscaled(value)
     return unscaled_value.to_bytes(bytes_required(unscaled_value), 
byteorder="big", signed=True)
+
+
+def truncate_decimal(value: Decimal, width: int) -> Decimal:
+    """Get a truncated Decimal value given a decimal value and a width
+    Args:
+        value (Decimal): a decimal value
+        width (int): A width for the returned Decimal instance
+    Returns:
+        Decimal: A truncated Decimal instance
+    """
+    unscaled_value = decimal_to_unscaled(value)
+    applied_value = unscaled_value - (((unscaled_value % width) + width) % 
width)

Review Comment:
   This expression doesn't need to be as complex. The purpose of this is to 
handle negative numbers in languages where `%` will return a negative value. 
Python returns positive values:
   
   ```python
   -4 % 5             # => 1
   ((-4 % 5) + 5) % 5 # => 1
   ```
   
   Note that if `-4 % 5` resulted in `-4` because `abs(-4) < 5` then we would 
need the more complex expression.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to