Skip to content

Commit b2ed759

Browse files
committed
Fix __round__ method replace format
1 parent 77b31de commit b2ed759

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

labfis/uncertainty.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,24 @@ def list(cls, listargs):
144144
listlabfloat = listlabfloat[0]
145145
return listlabfloat
146146

147-
def format(self):
147+
def __round__(self, p=0):
148148
current_contex = getcontext()
149149
setcontext(self.context)
150150

151151
u = Decimal(self._uncertainty)
152152
m = Decimal(self._mean)
153153

154-
u = round(u, -u.adjusted())
155-
m = round(m, -u.adjusted())
154+
p += -u.adjusted()*(not p)
155+
156+
u = round(u, p)
157+
m = round(m, p)
156158

157159
setcontext(current_contex)
158160

159161
return m, u
160162

161163
def split(self):
162-
m, u = self.format()
164+
m, u = self.__round__()
163165
return ["{:g}".format(m), "{:g}".format(u)]
164166

165167
def tex(self, *args, **kwargs):
@@ -187,7 +189,7 @@ def tex(self, *args, **kwargs):
187189

188190
if precision:
189191
precision = (str(precision[0]), str(precision[1]))
190-
m, u = self.format()
192+
m, u = self.__round__()
191193
m = eval("'{:." + precision[0] + "e}'.format(m)")
192194
u = eval("'{:." + precision[1] + "e}'.format(u)")
193195
else:
@@ -231,9 +233,6 @@ def __neg__(self):
231233
def __abs__(self):
232234
return labfloat(abs(self._mean), self._uncertainty)
233235

234-
def __round__(self, n):
235-
return labfloat(round(self._mean, n), round(self._uncertainty, n))
236-
237236
def __floor__(self):
238237
return labfloat(floor(self._mean), floor(self._uncertainty))
239238

0 commit comments

Comments
 (0)