Changeset 1903 in lmdz_wrf for trunk


Ignore:
Timestamp:
Apr 13, 2018, 3:12:54 PM (7 years ago)
Author:
lfita
Message:

Adding:

  • `Nstr': Function to transform a number to string, but making sure it preserves characteristics of the number
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1902 r1903  
    115115# multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string
    116116# Nomasked: Function to bring back a given array wthout the masked values reducing a given dimension
     117# Nstr: Function to transform a number to string, but making sure it preserves
     118#   characteristics of the number
    117119# Ntchar: Function to repeat a number of times a given tring
    118120# num_chainSnum: Function to pass a value to a `ChainStrNum' number
     
    1304213044    return newmatdate
    1304313045#print  advance_matDate([1976, 02, 27, 23, 59, 0], [0, 11, 0, 0, 2, 0])
     13046
     13047def Nstr(number):
     13048    """ Function to transform a number to string, but making sure it preserves
     13049      characteristics of the number
     13050    >>> Nstr(np.float(1./3.))
     13051    0.33333333
     13052    >>> Nstr(np.float32(1./3.))
     13053    0.33333334
     13054    >>> Nstr(np.float64(1./3.))
     13055    0.3333333333333333
     13056    """
     13057    fname = 'Nstr'
     13058
     13059    if type(number) == type(1) or type(number) == type(long)):
     13060        newStr = '{:d}'.format(number)
     13061    elif type(number) == type(np.int16(1)) type(number) == type(np.int32(1)) or      \
     13062      type(number) == type(np.int64(1)):
     13063        newStr = '{:d}'.format(number)
     13064    elif type(number) == type(float(1.)) or type(number) == type(np.float(1.)):
     13065        newStr = '{:.8f}'.format(number)
     13066    elif type(number) == type(np.float32(1.)):
     13067        newStr = '{:.8f}'.format(number) 
     13068    elif type(number) == type(np.float64(1.)):
     13069        newStr = '{:.16f}'.format(number) 
     13070
     13071    return newStr
     13072
     13073print Nstr(np.float(1./3.))
     13074
     13075print Nstr(np.float32(1./3.))
     13076
     13077print Nstr(np.float64(1./3.))
     13078
    1304413079#quit()
    1304513080
Note: See TracChangeset for help on using the changeset viewer.