Changeset 1410 for trunk/GSASIIpy3.py


Ignore:
Timestamp:
Jul 3, 2014 6:36:43 PM (7 years ago)
Author:
toby
Message:

fix number formatting in FormatValue? to be closer to Fortran, but always display a valid number, perhaps with few sig figs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpy3.py

    r1408 r1410  
    6363
    6464def 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.
    6667
    6768    :param float val: number to be formatted.
     
    7677    else:
    7778        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
    8386    decimals = digits[0] - digits[1]
    84     if abs(val) < 1e-99 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) < 1e-9 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**(4-decimals): # make sure at least 4 decimals show
     92        # this clause is probably no longer needed since the number probably shows as 0.0
    8893        decimals = min(digits[0]-5,digits[1])
    8994        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    90     elif abs(val) < 10**(4-decimals): # 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**(decimals-1): # deal with large numbers in smaller spaces
     96        decimals = max(0,digits[0]-5)
    9597        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    9698    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
    97100        decimals = min(digits[0]-3,digits[1])
    98101        fmt = "{" + (":{:d}.{:d}f".format(digits[0],decimals))+"}"
    99102    else: # in range where g formatting should do what I want
     103        # used?
    100104        decimals = digits[0] - 1
    101105        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
Note: See TracChangeset for help on using the changeset viewer.