maybe_decimal

Contents

maybe_decimal#

class brainunit.math.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 (saiunit.Quantity | Array | ndarray | bool | number | bool | int | float | complex) – The value to convert.

  • unit (saiunit.Unit | None) – 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:

Array | saiunit.Quantity

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