여기에 플로트 변환기가 없습니다. 그리고 나는 항상 세 자리로 그룹화를 놓쳤다.
할 것:
과학적인 표현에 - 민수기 (n.nnnnnn*10**(exp)
- (가) '10'
입니다self.baseDigits[1::-1]/self.to_string(len (self.baseDigits))
-from_string-function.
-base 1-> 로마 숫자?
agles와 복잡한 repr
그래서 여기 내 해결책이 있습니다 :
DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz"
# note that the order of the digits is reversed for digits before the point
NO_GROUPING = lambda g: g
concat = "".join
concat_backwards = lambda g: concat(e for e in reversed(list(g)))
def grouping(length = 3, char = '_'):
def yieldor(digits):
i = 0
for d in digits:
if i == length:
yield char
i = 0
yield d
i+=1
return yieldor
class Converter:
def __init__(self, baseDigits: (int, str), beforePoint = NO_GROUPING, afterPoint = NO_GROUPING, decimalPoint = '.', digitPrecision = 16, trimZeros = True):
if isinstance(baseDigits, int):
baseDigits = DIGITS[:baseDigits]
self.baseDigits = baseDigits
self.beforePoint = beforePoint
self.afterPoint = afterPoint
self.decimalPoint = decimalPoint
self.digitPrecision = digitPrecision
self.trimZeros = trimZeros
def to_string(self, number: (int, float, complex)) -> str:
if isinstance(number, complex):
if number.imag == 0:
return self.to_string(number.real)
if number.real == 0:
return self.to_string(number.imag) + 'j'
return "({}+{}j)".format(self.to_string(number.real), self.to_string (number.imag))
if number < 0:
return '-' + self.to_string(-number)
digitCount = len(self.baseDigits)
if isinstance(number, float):
# round correctly
precError=digitCount**-self.digitPrecision
number+=0.5*precError
if self.trimZeros:
def yieldor(n):
p = precError
for i in range(self.digitPrecision):
if n <= p:
return
p *= digitCount
n *= digitCount
digit = int(n)
n -= digit
yield self.baseDigits[digit]
else:
def yieldor(n):
for i in range(self.digitPrecision):
n *= digitCount
digit = int(n)
n -= digit
yield self.baseDigits[digit]
a = concat(self.afterPoint(yieldor(number%1)))
return (
self.to_string(int(number)) + (a and self.decimalPoint + a)
)
else: #is int
if not number: return self.baseDigits[0]
def yieldor(n):
while n:
n, digit = divmod(n, digitCount)
yield self.baseDigits[digit]
return concat_backwards(self.beforePoint(yieldor(number)))
# some tests:
if __name__ == "__main__":
def conv_test(num, digits, *argv, **kwv):
print(num, "->", digits if isinstance(digits, int) else "{} ({})".format(len(digits), digits), Converter(digits, *argv, **kwv).to_string(num))
conv_test(True, "ft")
conv_test(123, 12, grouping(2))
conv_test(-0xf00d, 16)
conv_test(1000, True<<True, grouping(4))
conv_test(1_000_000, "0+-", beforePoint = grouping(2, '|'))
conv_test(1.5, 10)
conv_test(0.999999999, 10, digitPrecision = 8)
conv_test(-0.1, 10)
import math
conv_test(math.pi, 10, afterPoint = grouping(5, ' '))
conv_test(0.123456789, 10, digitPrecision = 6)
grSpc = grouping(1, ' ')
conv_test(math.e, ["off", "on"], grSpc, grSpc, " dot ", digitPrecision = 7)
conv_test(1 + 1.5j, 10)
conv_test(50j, 10)
conv_test(10.01, '-<>')
# and generate some brainfuck-code here:
conv_test(1701**42, '+-<>,.][', digitPrecision = 32)