From 815f709c7bc32cd35195de89b4a1cd2a53433839 Mon Sep 17 00:00:00 2001 From: MMGen Date: Mon, 27 May 2019 09:29:28 +0000 Subject: [PATCH] BTCAmt: add 'from_decimal' option to constructor --- mmgen/obj.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mmgen/obj.py b/mmgen/obj.py index b7ab1387..ecce9752 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -390,7 +390,8 @@ class BTCAmt(Decimal,Hilite,InitErrors): units = ('satoshi',) forbidden_types = (float,int) - def __new__(cls,num,from_unit=None,on_fail='die'): + # NB: 'from_decimal' rounds down to precision of 'min_coin_unit' + def __new__(cls,num,from_unit=None,from_decimal=False,on_fail='die'): if type(num) == cls: return num cls.arg_chk(on_fail) try: @@ -399,6 +400,10 @@ class BTCAmt(Decimal,Hilite,InitErrors): "'{}': unrecognized denomination for {}".format(from_unit,cls.__name__)) assert type(num) == int,'value is not an integer or long integer' me = Decimal.__new__(cls,num * getattr(cls,from_unit)) + elif from_decimal: + assert type(num) == Decimal,( + "number is not of type Decimal (type is {!r})".format(type(num).__name__)) + me = Decimal.__new__(cls,num).quantize(cls.min_coin_unit) else: for t in cls.forbidden_types: assert type(num) is not t,"number is of forbidden type '{}'".format(t.__name__)