Changeset 1177 for trunk/GSASIIpy3.py


Ignore:
Timestamp:
Dec 19, 2013 1:34:54 PM (8 years ago)
Author:
vondreele
Message:

try to add more control over number format from ValidatedTxtCtrl?
not entirely successful so far

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpy3.py

    r1077 r1177  
    3939    return val
    4040
    41 def FormatValue(val,maxdigits=10):
    42     '''Format a float to fit in ``maxdigits`` spaces, showing as much
    43     precision as possible, more or less.
     41def FormatValue(val,maxdigits=[10,2]):
     42    '''Format a float to fit in ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
    4443
    4544    :param float val: number to be formatted.
    4645
    47     :param int maxdigits: the number of digits to be used for display of the
    48       number (defaults to 10).
     46    :param list maxdigits: the number of digits & places after decimal to be used for display of the
     47      number (defaults to [10,2]).
    4948
    5049    :returns: a string with <= maxdigits characters (I hope). 
     
    5251    # does the standard str() conversion fit?
    5352    string = str(val)
    54     if len(string) <= maxdigits: return string.strip()
     53    if len(string) <= maxdigits[0]: return string.strip()
    5554    # negative numbers, leave room for a sign
    56     if val < 0: maxdigits -= 1
    57     decimals = maxdigits - 2
     55    if val < 0: maxdigits[0] -= 1
     56    decimals = maxdigits[0] - maxdigits[1]
    5857    if abs(val) < 1e-99 or abs(val) > 1e99:
    59         decimals = maxdigits - 6
    60         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
     58        decimals = min(maxdigits[0]-6,maxdigits[1])
     59        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}" # create format string
    6160    elif abs(val) < 1e-9 or abs(val) > 1e9:
    62         decimals = maxdigits - 5
    63         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     61        decimals = min(maxdigits[0]-5,maxdigits[1])
     62        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    6463    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
    65         decimals = maxdigits - 5
    66         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     64        decimals = min(maxdigits[0]-5,maxdigits[1])
     65        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    6766    elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces
    68         decimals = maxdigits - 5
    69         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     67        decimals = min(maxdigits[0]-5,maxdigits[1])
     68        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
    7069    elif abs(val) < 1: # use f format for small numbers
    71         decimals = maxdigits - 2
    72         fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
     70        decimals = min(maxdigits[0]-3,maxdigits[1])
     71        fmt = "{" + (":{:d}.{:d}f".format(maxdigits[0],decimals))+"}"
    7372    else: # in range where g formatting should do what I want
    74         decimals = maxdigits - 1
    75         fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     73        decimals = maxdigits[0] - 1
     74        fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     75    print fmt,val
    7676    return fmt.format(val).strip()
Note: See TracChangeset for help on using the changeset viewer.