Skip to content


Repository files navigation

engfmt - Engineering Format


engfmt has been deprecated and replaced by QuantiPhy.

A light-weight package used to read and write numbers in engineering format. In engineering format a number generally includes the units if available and uses SI scale factors to indicate the magnitude of the number. For example:


A quantity is the pairing of a real number and units, though the units may be empty. This package is designed to convert quantities between the various ways in which they are represented. Those ways are:

As a tuple:

For example, 1ns would be represented as (1e-9, 's'). Notice that the scale factor is not included in the units. This is always true.

As a string in conventional formats:

For example, 1ns would be represented as '1e-9 s' or as '0.000000001s'. This form is often difficult to read for people and so engfmt treats it more as a format meant for machines rather than people.

As a string in engineering format:

For example, 1ns would be represented as '1ns'. This form is often difficult to read for machines and so engfmt treats it more as a human readable format.

The Quantity class is provided for converting between these various forms. It takes one or two arguments. The first is taken to be the value, and the second, if given, is taken to be the units. The value may be given as a float or as a string. The string may be in floating point notation, in scientific notation, or in engineering format and may include the units. By engineering notation, it is meant that the number can use the SI scale factors. For example, any of the following ways can be used to specify 1ns:

In all cases, the giving the units is optional.

From a quantity object, you can generate any representation:

You can also access the value without the units:

>>> h_line.to_float()

>>> h_line.to_unitless_eng()

>>> h_line.to_unitless_str()

Or you can access just the units:

>>> h_line.units

The output of the to_eng and to_unitless_eng methods is always rounded to the desired precision, which can be specified as an argument. This differs from the to_str and to_unitless_str methods. They attempt to retain the original format of the number if it is specified as a string. In this way it retains its original precision. The underlying assumption behind this difference is that engineering notation is generally used when communicating with people, whereas floating point notation is used when communicating with machines. People benefit from having a limited number of digits in the numbers, whereas machines benefit from have full precision numbers.

Quantities As Reals

You can use a quantity in the same way that you can use a real number, meaning that you can use it in expressions and it will evaluate to its real value:

>>> period = Quantity('1us')
>>> print(period)

>>> frequency = 1/period
>>> print(frequency)

Notice that when performing arithmetic operations on quantities the units are completely ignored.

Shortcut Functions

Generally one uses the shortcut functions to convert numbers to and from engineering format. All of these functions take the value and units in the same ways that they are specified to Quantity. In particular, the value may be a string or a real number. If it is a string it may be given in traditional format or in engineering format, and it may include the units. For example:


You can adjust some of the behavior of these functions on a global basis using set_preferences:

Specifying hprec (human precision) to be 4 gives 5 digits of precision (you get one more digit than the number you specify for precision). Thus, the valid range for prec is from 0 to around 12 to 14 for double precision numbers.

Passing None as a value in set_preferences returns that preference to its default value:

The available preferences are:

hprec (int):

Human precision in digits where 0 corresponds to 1 digit, must be nonnegative. This precision is used when generating engineering format.

mprec (int):

Machine precision in digits where 0 corresponds to 1 digit. Must be nonnegative. This precision is used when not generating engineering format.

spacer (str):

May be '' or ' ', use the latter if you prefer a space between the number and the units. Generally using ' ' makes numbers easier to read, particularly with complex units, and using '' is easier to parse.

unity (str):

The output scale factor for unity, generally '' or '_'.

output (str):

Which scale factors to output, generally one would only use familiar scale factors.

ignore_sf (bool):

Whether scale factors should be ignored by default.

assign_fmt (str):

Format string for an assignment. Will be passed through string format method. Format string takes three possible arguments named n, q, and d for the name, value and description. The default is '{n} = {v}'

assign_rec (str):

Regular expression used to recognize an assignment. Used in add_to_namespace(). Default recognizes the form:

"Temp = 300_K -- Temperature".

Quantity Class

Though rarely used directly, the Quantity class forms the foundation of the engfmt package. It is more flexible than the shortcut functions:

Physical Constants

The Quantity class also supports a small number of physical constants.

Plank's constant:

Boltzmann's constant:

Elementary charge:

Speed of light:

Zero degrees Celsius in Kelvin:

engfmt uses k rather than K to represent kilo so that you can distinguish between kilo and Kelvin.

Permittivity of free space:

Permeability of free space:

Characteristic impedance of free space:

You can add additional constants by adding them to the CONSTANTS dictionary:

String Formatting

Quantities can be passed into the string format method:

You can specify the precision as part of the format specification

The 'q' type specifier can be used to explicitly indicate that both the number and the units are desired:

Alternately, 'r' can be used to indicate just the number is desired:

Use 'u' to indicate that only the units are desired:

You can also use the string and floating point format type specifiers:

It is also possible to add a name and perhaps a description to the quantity, and access those with special format codes as well:


A ValueError is raised if engfmt is passed a string it cannot convert into a number:

Text Processing

Two functions are available for converting quantities embedded within text to and from engineering notation:

Add to Namespace

It is possible to put a collection of quantities in a text string and then use the add_to_namespace function to parse the quantities and add them to the Python namespace. For example:

Any number of quantities may be given, with each quantity given on its own line. The identifier given to the left '=' is the name of the variable in the local namespace that is used to hold the quantity. The text after the '--' is used as a description of the quantity.

Scale Factors and Units

By default, engfmt treats both the scale factor and the units as being optional. With the scale factor being optional, the meaning of some specifications can be ambiguous. For example, '1m' may represent 1 milli or it may represent 1 meter. Similarly, '1meter' my represent 1 meter or 1 milli-eter. To allow you to avoid this ambiguity, engfmt accepts '_' as the unity scale factor. In this way '1_m' is unambiguously 1 meter. You can instruct engfmt to output '_' as the unity scale factor by specifying the unity argument to set_preferences:

If you need to interpret numbers that have units and are known not to have scale factors, you can specify the ignore_sf preference:


Use 'pip install engfmt' to install. Requires Python2.7 or Python3.3 or better.



Be aware that engfmt has been deprecated in favor of quantiphy.


Run 'py.test' to run the tests.


Read and write in engineering notation







No releases published


No packages published