我想将输入的数字格式化为保留两到三位小数的浮点型和定点型数字,但是我的代码无法实现。
这会产生以下错误信息:
class Quake:
"""Earthquake in terms of latitude, longitude, depth and magnitude"""
def __init__(self, lat, lon, depth, mag):
self.lat=lat
self.lon=lon
self.depth=depth
self.mag=mag
def __str__(self):
return "M{2.2f}, {3.2f} km, lat {3.3f}\N{DEGREE\
SIGN lon {3.3f}\N{DEGREE SIGN}".format(
self.mag, self.depth, self.lat, self.lon)
这会产生以下错误信息:
'AttributeError: 'float' object has no attribute '2f''
format
的方式不正确:http://docs.python.org/library/string.html#formatspec - Ashwini Chaudhary