Sujet : Re: FAA To Finally Ditch Floppy Disks & Win-95
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.os.linux.miscDate : 24. Jun 2025, 07:47:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103dhmr$1rlr3$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
User-Agent : Pan/0.162 (Pokrosvk)
On Tue, 24 Jun 2025 04:52:22 GMT, Charlie Gibbs wrote:
On 2025-06-24, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
Very few languages include fixed-point types (with a fractional part)
as standard -- Ada being an exception, as I recall.
Meanwhile we have to fake it:
printf("%8ld.%02d\n", amount / 100L, amount % 100L);
>
Handling negative amounts is left as an exercise for the user.
Some languages don’t need to “fake” it:
import decimal as dec
from decimal import \
Decimal
D = Decimal
DECIMALS = 2
def fmt(d : Decimal) :
# returns string representation of d with DECIMALS decimal places
if isinstance(d, Decimal) :
pass
elif isinstance(d, int) :
d = D(d)
else :
raise TypeError("arg must be of Decimal type")
#end if
if d < 0 :
sign = "-"
d = - d
else :
sign = ""
#end if
s = str(int(round(d, DECIMALS) * D('10') ** DECIMALS))
if len(s) > DECIMALS :
s = s[:len(s) - DECIMALS] + "." + s[len(s) - DECIMALS:]
else :
s = "0." + ("0" * (DECIMALS - len(s))) + s
#end if
return sign + s
#end fmt