sidpy.base.string_utils.formatted_str_to_number

sidpy.base.string_utils.formatted_str_to_number(str_val, magnitude_names, magnitude_values, separator=' ')[source]

Takes a formatted string like ‘4.32 MHz’ to 4.32 E+6. This function provides the inverse functionality of sidpy.string_utils.format_quantity

Parameters:
  • str_val (str / unicode) – String value of the quantity. Example ‘4.32 MHz’

  • magnitude_names (Iterable) – List of names of units like [‘seconds’, ‘minutes’, ‘hours’]

  • magnitude_values (Iterable) – List of values (corresponding to magnitude_names) that scale the numeric value. Example [1, 60, 3600]

  • separator (str / unicode, optional. Default = ' ' (space)) – The text that separates the numeric value and the units.

Returns:

Numeric value of the string

Return type:

number

Examples

>>> import sidpy
>>> unit_names = ["MHz", "kHz"]
>>> unit_magnitudes = [1E+6, 1E+3]
>>> str_value = "4.32 MHz"
>>> num_value = sidpy.string_utils.formatted_str_to_number(str_value,
>>>                                                       unit_names,
>>>                                                       unit_magnitudes,
>>>                                                       separator=' ')
>>> print('formatted_str_to_number: {} = {}'.format(str_value, num_value))

See also

sidpy.string_utils.format_quantity