Skip to content
Snippets Groups Projects
Commit e634953f authored by Daniel Brown's avatar Daniel Brown
Browse files

Changing SIfloat to only convert last character of the string

parent 183a2756
No related branches found
No related tags found
No related merge requests found
import os
import re
import pykat.exceptions as pkex
#staticmethod
def SIfloat(value):
if value==None:
return value
if type(value)==list:
return [convertToFloat(s) for s in value]
else:
return convertToFloat(value)
def convertToFloat(value):
__suffix = {'y': 'e-24', # yocto
'z': 'e-21', # zepto
'a': 'e-18', # atto
......@@ -28,9 +19,35 @@ def convertToFloat(value):
'P': 'e15' # peta
}
value = str(value)
def SIfloat(value):
if value==None:
return value
if type(value)==list:
return [convertToFloat(s) for s in value]
else:
return convertToFloat(value)
def convertToFloat(value):
try:
# first just try and convert the value
return float(value)
for i, j in __suffix.iteritems():
value=value.replace(i, str(j))
except ValueError as ex:
# Catch any casting exeception
value = value.strip()
# only the last value can be an SI scaling letter
last = value[-1]
if last in __suffix:
# remove last character and append the SI scaling
value = value[0:-1] + __suffix[last]
else:
raise pkex.BasePyKatException("Could not convert SI scaling in '{0}' to a float".format(value))
try:
return float(value)
except ValueError as ex:
raise pkex.BasePyKatException("Unable to convert '{0}' into a float".format(value))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment