Changeset 1410 for trunk/GSASIIpy3.py
 Timestamp:
 Jul 3, 2014 6:36:43 PM (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/GSASIIpy3.py
r1408 r1410 63 63 64 64 def FormatValue(val,maxdigits=None): 65 '''Format a float to fit in ``maxdigits[0]`` spaces with maxdigits[1] after decimal. 65 '''Format a float to fit in at most ``maxdigits[0]`` spaces with maxdigits[1] after decimal. 66 Note that this code has been hacked from FormatSigFigs and may have unused sections. 66 67 67 68 :param float val: number to be formatted. … … 76 77 else: 77 78 digits = list(maxdigits) 78 # does the standard str() conversion fit? 79 string = str(val) 80 if len(string) <= digits[0]: return string.strip() 81 # negative numbers, leave room for a sign 82 if val < 0: digits[0] = 1 79 string = ("{:."+str(digits[1])+"f}").format(val).strip() # will standard .f formatting work? 80 if len(string) <= digits[0]: 81 if digits[1] > 0: # strip off extra zeros on right side 82 string = string.rstrip('0') 83 if string[1] == '.': string += "0" 84 return string 85 if val < 0: digits[0] = 1 # negative numbers, reserve space for the sign 83 86 decimals = digits[0]  digits[1] 84 if abs(val) < 1e99 or abs(val) > 1e99: 85 decimals = min(digits[0]6,digits[1]) 86 fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}" # create format string 87 elif abs(val) < 1e9 or abs(val) > 1e9: 87 if abs(val) > 1e99: # for very large numbers, use scientific notation and use all digits 88 fmt = "{" + (":{:d}.{:d}g".format(digits[0],digits[0]6))+"}" 89 elif abs(val) > 1e9: 90 fmt = "{" + (":{:d}.{:d}g".format(digits[0],digits[0]5))+"}" 91 elif abs(val) < 10**(4decimals): # make sure at least 4 decimals show 92 # this clause is probably no longer needed since the number probably shows as 0.0 88 93 decimals = min(digits[0]5,digits[1]) 89 94 fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}" 90 elif abs(val) < 10**(4decimals): # make sure at least 4 decimals show 91 decimals = min(digits[0]5,digits[1]) 92 fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}" 93 elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces 94 decimals = min(digits[0]5,digits[1]) 95 elif abs(val) >= 10**(decimals1): # deal with large numbers in smaller spaces 96 decimals = max(0,digits[0]5) 95 97 fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}" 96 98 elif abs(val) < 1: # use f format for small numbers 99 # this clause is probably no longer needed since the number probably shows as 0.0 97 100 decimals = min(digits[0]3,digits[1]) 98 101 fmt = "{" + (":{:d}.{:d}f".format(digits[0],decimals))+"}" 99 102 else: # in range where g formatting should do what I want 103 # used? 100 104 decimals = digits[0]  1 101 105 fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
Note: See TracChangeset
for help on using the changeset viewer.