saiunit.maybe_decimal

Contents

saiunit.maybe_decimal#

saiunit.maybe_decimal(val, unit=None)#

Convert a quantity to a plain number if it is dimensionless.

If val has physical dimensions and no unit is provided, the original Quantity is returned unchanged.

Parameters:
  • val (Union[saiunit.Quantity, Array, ndarray, number, bool]) – The value to convert.

  • unit (Optional[saiunit.Unit]) – If provided, convert val to this unit before stripping the unit.

Returns:

decimal – A plain number when val is dimensionless (or convertible via unit), otherwise the original Quantity.

Return type:

Union[Array, saiunit.Quantity, ndarray, number, bool]

Examples

>>> import saiunit as u
>>> u.maybe_decimal(u.Quantity(5.0))
5.0
>>> q = 1.0 * u.metre
>>> u.maybe_decimal(q) == q       # not dimensionless, returned as-is
True