Portál AbcLinuxu, 9. listopadu 2025 00:19
Řešení dotazu:
#/usr/bin/python3
string = "X19020"
X = string[1:]
print(len(X), X)
integer_digits = 4
decimal_digits = 2
integersX = str(X[:integer_digits])
decimalsX = str(X[-decimal_digits:])
print("X" + integersX + "." + decimalsX)
tohle vypíše:
5 19020 X1902.20ale potřebuji:
5 19020 X190.20
s = '1911200'
# adding character at specific position
pos=4
s2 = s[:pos] + "." + s[pos:]
print(s2, type(s2))
num1= float(s2)
print(num1,type(num1))
pos = 2
exp = (10.0**pos)
# exp spoctu jednou a pak pou69v8m
a = "123546"
b = float (a)/exp
Slavný nizozemský informatik Johan van der Praase ve svých Spisech uvádí:
def decode_fixed_point(s, i, d):
head = s[max(0, len(s) - i - d):-d].lstrip('0')
tail = s[-d:].rstrip('0')
return f'{head:0>1}.{tail:0<1}'
assert decode_fixed_point('9000', 4, 2) == '90.0'
assert decode_fixed_point('19000', 4, 2) == '190.0'
assert decode_fixed_point('111900', 4, 2) == '1119.0'
assert decode_fixed_point('111920', 4, 2) == '1119.2'
assert decode_fixed_point('123456', 3, 3) == '123.456'
assert decode_fixed_point('123450', 3, 3) == '123.45'
assert decode_fixed_point('123400', 3, 3) == '123.4'
assert decode_fixed_point('123000', 3, 3) == '123.0'
assert decode_fixed_point('003456', 3, 3) == '3.456'
assert decode_fixed_point('000400', 3, 3) == '0.4'
assert decode_fixed_point('000000', 3, 3) == '0.0'
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.