Hãy xem xét mã tự nhận xét sau. Tôi cố gắng để giữ trong đơn giản
>>> from fractions import Fraction
>>> def Arch2Float(num):
#First Partition from Right so that the Feet and Unit always
#Remains aligned even if one of them is absent
ft,x,inch=num.rpartition("\'")
#Convert the inch to a real and frac part after stripping the
#inch (") identifier. Note it is assumed that the real and frac
#parts are delimited by '-'
real,x,frac=inch.strip("\"").rpartition("-")
#Now Convert every thing in terms of feet which can then be converted
#to float. Note to trap Error's like missing or invalid items, its better
#to convert each items seperately
result=0
try:
result = int(ft.strip("\'"))
except ValueError:
None
#Convert the real inch part as a fraction of feet
try:
result += Fraction(int(real),12)
except ValueError:
None
#Now finally convert the Fractional part using the fractions module and convert to feet
try:
result+=Fraction(frac)/12
except ValueError:
None
return float(result)
Acid Test
>>> print Arch2Float('15-3/4"') # 15-3/4" (without ft)
1.3125
>>> print Arch2Float('12\' 6-3/4"') #12' 6-3/4"
12.5625
>>> print Arch2Float('12\'6-3/4"') #12'6-3/4" (without space)
12.5625
>>> print Arch2Float('3/4"') #3/4" (just the inch)
0.0625
>>> print Arch2Float('15\'') #15' (just ft)
15.0
>>> print Arch2Float('15') #15 (without any ascent considered as inch)
1.25
Chuyển đổi từ phao để Kiến trúc sẽ dễ dàng như bạn không phải đi nỗi đau để phân tích cú pháp
>>> def Float2Arch(num):
num=Fraction(num)
ft,inch=Fraction(num.numerator/num.denominator),Fraction(num.numerator%num.denominator)/num.denominator*12
real,frac=inch.numerator/inch.denominator,Fraction(inch.numerator%inch.denominator,inch.denominator)
return '{0}\' {1}-{2}"'.format(ft,real,frac)
Acid Kiểm tra
>>> print Float2Arch(Arch2Float('12\' 6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('15-3/4"'))
1' 3-3/4"
>>> print Float2Arch(Arch2Float('12\'6-3/4"'))
12' 6-3/4"
>>> print Float2Arch(Arch2Float('3/4"'))
0' 0-3/4"
>>> print Float2Arch(Arch2Float('15\''))
15' 0-0"
>>> print Float2Arch(Arch2Float('15'))
1' 3-0"
>>>
Lưu ý *** Điều quan trọng của nó là giữ cho biểu diễn dạng float trong mẫu số thấp nhất (inch) hoặc mẫu số đại diện cao nhất (feet). Tôi đã chọn mức cao nhất trong trường hợp này. Nếu bạn wan't để hạ nó, bạn có thể nhân nó bằng 12.
Cập nhật để phục vụ làm tròn Request (Không chắc chắn nếu đây là thanh lịch nhưng hiện các công việc)
def Float2Arch(num):
num=Fraction(num)
ft,inch=Fraction(num.numerator/num.denominator),Fraction(num.numerator%num.denominator)/num.denominator*12
real,frac=inch.numerator/inch.denominator,Fraction(inch.numerator%inch.denominator,inch.denominator)
for i in xrange(1,17):
if Fraction(frac) < Fraction(1.0/16*i): break
frac=Fraction(1.0/16*i)
if frac>= 1:
real+=1
frac=0
return '{0}\' {1}-{2}"'.format(ft,real,frac)
Tất nhiên có một cách. Nhưng đó là những giá trị với các đơn vị, do đó, chuyển đổi trực tiếp sang điểm động cũng sẽ yêu cầu xác định đơn vị ngụ ý mà nó sẽ được. bạn cũng có thể mất độ chính xác. – Keith
Ngoài ra, Django chỉ là một khuôn khổ web, do đó, nó thực sự không có bất cứ điều gì để làm với điều này. Bây giờ bạn có thể tạo một đối tượng, khi được xâu chuỗi tạo ra định dạng đó. Sau đó, bạn chỉ có thể sử dụng bất kỳ mẫu nào. – Keith
Cảm ơn Keith ... Tôi đã xóa phần Django. – Clayton