@@ -602,6 +602,42 @@ numbers by creating a ``Encoder`` and specifying ``decimal_format='number'``.
602602 >> > encoder.encode(x)
603603 b ' 1.2345'
604604
605+ You may also optionally specify a target precision for rounding
606+ Decimal values before encoding using the ``decimal_quantize `` parameter.
607+ If provided, all `decimal.Decimal`` values will be quantized (rounded)
608+ to the same scale as this value using the `Decimal.quantize() ` method.
609+ This is useful for ensuring consistent precision of decimal values
610+ during serialization, particularly in financial or monetary contexts.
611+
612+ .. code-block :: python
613+
614+ >> > import decimal
615+
616+ >> > encoder = msgspec.json.Encoder(decimal_quantize = decimal.Decimal(" 0.00" ))
617+
618+ >> > encoder.encode(decimal.Decimal(" 1.23456789" ))
619+ b ' "1.23"'
620+
621+ The optional ``decimal_rounding `` parameter allows you to specify
622+ the rounding mode to use when quantizing Decimal values. It accepts
623+ one of the standard rounding mode strings from Python's decimal module,
624+ such as ``'ROUND_DOWN' `` , ``'ROUND_HALF_UP' `` , etc. If not specified,
625+ the default rounding mode is used (``'ROUND_HALF_EVEN' ``).
626+
627+ .. code-block :: python
628+
629+ >> > encoder = msgspec.json.Encoder(
630+ ... decimal_quantize = decimal.Decimal(" 0.00" ),
631+ ... decimal_rounding = decimal.ROUND_UP ,
632+ ... )
633+
634+ >> > encoder.encode(decimal.Decimal(" 1.235" )) # Rounded up to two decimal places
635+ b ' "1.24"'
636+
637+ .. note ::
638+
639+ This parameter has no effect unless ``decimal_quantize `` is also specified.
640+
605641This setting is not yet supported for YAML or TOML - if this option is
606642important for you please `open an issue `_.
607643
0 commit comments