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", "openff.units", "string", "record"}, optional) – Target backend form. If omitted, the input form is preserved.
"record"is the inert interchange form (pyunitwizard.record.QuantityRecord).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".
Notes
Conversions within PyUnitWizard’s Pint registry between verified standard nanometer, angstrom and picometer units round once for binary64 magnitudes. This applies to float64 arrays and to integer scalars exactly representable in binary64. Other conversions use the selected backend’s arithmetic.
Examples
>>> import pyunitwizard as puw >>> q = puw.quantity(1.0, "nanometer") >>> puw.convert(q, to_unit="angstrom")