pyunitwizard.convert#
- pyunitwizard.convert(quantity_or_unit, to_unit=None, to_form=None, parser=None, to_type='quantity')[source]#
Convert a quantity or unit across unit systems, forms, and output types.
- Parameters:
quantity_or_unit (Any) – Input quantity or unit to convert. It can be any supported runtime form, including strings when a compatible parser is configured.
to_unit (str, optional) – Target unit. When provided, the quantity is converted to this unit before returning.
to_form ({"unyt", "pint", "openmm.unit", "astropy.units", "string"}, optional) – Target backend form. If omitted, the input form is preserved.
parser ({"pint", "openmm.unit", "astropy.units"}, optional) – Parser used when quantity_or_unit or to_unit is provided as a string.
to_type ({"quantity", "unit", "value"}, optional, default="quantity") – Output type to return.
- Returns:
Converted object in the requested unit/form/type.
- Return type:
QuantityOrUnit or float or numpy.ndarray
- Raises:
BadCallError – If to_type is not one of
"quantity","unit", or"value".
Examples
>>> import pyunitwizard as puw >>> q = puw.quantity(1.0, "nanometer") >>> puw.convert(q, to_unit="angstrom")