Skip to content

Commit 9c8c493

Browse files
authored
Merge pull request #93 from digital-asset/python-decimal-formatting
python: Support formatting decimals that are very small.
2 parents dfe29df + ecd203a commit 9c8c493

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.8.1
1+
6.8.2

python/dazl/protocols/v1/pb_ser_command.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
from ... import LOG
1111
# noinspection PyPep8Naming
1212
from . import model as G
13-
from ...damlast.daml_lf_1 import TypeConName
1413
from ...model.core import ContractId
1514
from ...model.types import VariantType, RecordType, ListType, ContractIdType, \
1615
UnsupportedType, TemplateChoice, TypeReference, TypeEvaluationContext, SCALAR_TYPE_UNIT, \
1716
TextMapType, OptionalType, EnumType
1817
from ...model.writing import AbstractSerializer, CommandPayload
1918
from ...util.prim_types import to_boolean, to_date, to_datetime, to_decimal, to_int, to_str, \
20-
decode_variant_dict
19+
to_ledger_api_decimal, decode_variant_dict
2120

2221
# noinspection PyPackageRequirements
2322
from google.protobuf.empty_pb2 import Empty
@@ -154,7 +153,7 @@ def serialize_int(self, context: TypeEvaluationContext, obj: Any) -> R:
154153
return 'int64', to_int(obj)
155154

156155
def serialize_decimal(self, context: TypeEvaluationContext, obj: Any) -> R:
157-
return 'numeric', str(to_decimal(obj))
156+
return 'numeric', to_ledger_api_decimal(to_decimal(obj))
158157

159158
def serialize_party(self, context: TypeEvaluationContext, obj: Any) -> R:
160159
return 'party', to_str(obj)

python/dazl/util/prim_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def to_boolean(obj) -> bool:
4646
raise ValueError(f'Could not parse as a boolean: {obj!r}')
4747

4848

49+
def to_ledger_api_decimal(obj: 'Decimal') -> str:
50+
precision = max(0, -obj.as_tuple().exponent)
51+
return format(obj, f'.{precision}f')
52+
53+
4954
@overload
5055
def to_timedelta(obj: TimeDeltaConvertible) -> timedelta: ...
5156

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "dazl"
3-
version = "6.8.1"
3+
version = "6.8.2"
44
description = "high-level Ledger API client for DAML ledgers"
55
license = "Apache-2.0"
66
authors = ["Davin K. Tanabe <davin.tanabe@digitalasset.com>"]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from decimal import Decimal
2+
3+
from dazl.util.prim_types import to_ledger_api_decimal
4+
5+
6+
def test_one():
7+
assert to_ledger_api_decimal(Decimal("1")) == "1"
8+
9+
10+
def test_ten():
11+
assert to_ledger_api_decimal(Decimal("10")) == "10"
12+
13+
14+
def test_one_tenth():
15+
assert to_ledger_api_decimal(Decimal("0.1")) == "0.1"
16+
17+
18+
def test_one_trillionth():
19+
assert to_ledger_api_decimal(Decimal("0.000000000001")) == "0.000000000001"
20+
21+
22+
def test_one_trillion():
23+
assert to_ledger_api_decimal(Decimal("1000000000000")) == "1000000000000"
24+
25+
26+
def test_one_trillionth_and_a_bit():
27+
assert to_ledger_api_decimal(Decimal("0.0000000000011")) == "0.0000000000011"
28+
29+
30+
def test_quarter_quadrillion():
31+
assert to_ledger_api_decimal(Decimal("250000000000000")) == "250000000000000"
32+
33+
34+
def test_quarter_quadrillion_and_a_bit():
35+
assert to_ledger_api_decimal(Decimal("250000000000000.0000000000011")) == "250000000000000.0000000000011"
36+

0 commit comments

Comments
 (0)