comparison babel/numbers.py @ 584:e0454b9c125c trunk

fix indentation in split_number
author fschwarz
date Sat, 04 Aug 2012 20:22:49 +0000
parents ae897a807442
children 964cd2ec6f94
comparison
equal deleted inserted replaced
583:ae897a807442 584:e0454b9c125c
321 SUFFIX_PATTERN)) 321 SUFFIX_PATTERN))
322 322
323 def split_number(value): 323 def split_number(value):
324 """Convert a number into a (intasstring, fractionasstring) tuple""" 324 """Convert a number into a (intasstring, fractionasstring) tuple"""
325 if isinstance(value, Decimal): 325 if isinstance(value, Decimal):
326 # NB can't just do text = str(value) as str repr of Decimal may be 326 # NB can't just do text = str(value) as str repr of Decimal may be
327 # in scientific notation, e.g. for small numbers. 327 # in scientific notation, e.g. for small numbers.
328 328
329 sign, digits, exp = value.as_tuple() 329 sign, digits, exp = value.as_tuple()
330 # build list of digits in reverse order, then reverse+join 330 # build list of digits in reverse order, then reverse+join
331 # as per http://docs.python.org/library/decimal.html#recipes 331 # as per http://docs.python.org/library/decimal.html#recipes
332 int_part = [] 332 int_part = []
333 frac_part = [] 333 frac_part = []
334 334
335 digits = map(str, digits) 335 digits = map(str, digits)
336 336
337 # get figures after decimal point 337 # get figures after decimal point
338 for i in range(-exp): 338 for i in range(-exp):
339 # add digit if available, else 0 339 # add digit if available, else 0
340 if digits: 340 if digits:
341 frac_part.append(digits.pop()) 341 frac_part.append(digits.pop())
342 else: 342 else:
343 frac_part.append('0') 343 frac_part.append('0')
344 344
345 # add in some zeroes... 345 # add in some zeroes...
346 for i in range(exp): 346 for i in range(exp):
347 int_part.append('0') 347 int_part.append('0')
348 348
349 # and the rest 349 # and the rest
350 while digits: 350 while digits:
351 int_part.append(digits.pop()) 351 int_part.append(digits.pop())
352 352
353 # if < 1, int_part must be set to '0' 353 # if < 1, int_part must be set to '0'
354 if len(int_part) == 0: 354 if len(int_part) == 0:
355 int_part = '0', 355 int_part = '0',
356 356
357 if sign: 357 if sign:
358 int_part.append('-') 358 int_part.append('-')
359 359
360 return ''.join(reversed(int_part)), ''.join(reversed(frac_part)) 360 return ''.join(reversed(int_part)), ''.join(reversed(frac_part))
361 text = ('%.9f' % value).rstrip('0') 361 text = ('%.9f' % value).rstrip('0')
362 if '.' in text: 362 if '.' in text:
363 a, b = text.split('.', 1) 363 a, b = text.split('.', 1)
364 if b == '0': 364 if b == '0':
365 b = '' 365 b = ''
Copyright (C) 2012-2017 Edgewall Software