Skip to content

Commit

Permalink
added fix_nr_mileage_str() to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeqfu committed Sep 8, 2020
1 parent ef4f533 commit bc9ec4d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pyrcs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,44 @@ def fix_num_stanox(stanox_code):
return stanox_code


def fix_nr_mileage_str(nr_mileage):
"""
Fix NR mileage.
:param nr_mileage: NR mileage
:type nr_mileage: str, float
:return: conventional NR mileage code
:rtype: str
**Examples**::
nr_mileage = 29.011
fix_nr_mileage_str(nr_mileage) # '29.0110'
nr_mileage = '.1100'
fix_nr_mileage_str(nr_mileage) # '0.1100'
"""

if isinstance(nr_mileage, float):
nr_mileage_ = fix_nr_mileage_str(str(nr_mileage))

elif nr_mileage and nr_mileage != '0':
if '.' in nr_mileage:
miles, yards = nr_mileage.split('.')
if miles == '':
miles = '0'
else:
miles, yards = nr_mileage, '0'
if len(yards) < 4:
yards += '0' * (4 - len(yards))
nr_mileage_ = '.'.join([miles, yards])

else:
nr_mileage_ = nr_mileage

return nr_mileage_


# -- Misc ---------------------------------------------------------------------------------------------

def is_str_float(str_val):
Expand Down

0 comments on commit bc9ec4d

Please sign in to comment.