跳转至

molop.unit

本页面提供了 molop.unit 模块的 API 参考。

Author: TMJ Date: 2024-10-19 09:57:26 LastEditors: TMJ LastEditTime: 2025-12-16 01:12:17 Description: 请填写简介

unit_transform(value, unit)

Transform the unit of a quantity.

Parameters:

Name Type Description Default
value Optional[PlainQuantity]

The quantity to be transformed.

required
unit UnitLike

The target unit.

required

Returns:

Type Description
PlainQuantity | None

Optional[PlainQuantity]: The transformed quantity.

Source code in src/molop/unit/__init__.py
Python
def unit_transform(value: PlainQuantity | None, unit: UnitLike) -> PlainQuantity | None:
    """
    Transform the unit of a quantity.

    Parameters:
        value (Optional[PlainQuantity]): The quantity to be transformed.
        unit (UnitLike): The target unit.

    Returns:
        Optional[PlainQuantity]: The transformed quantity.
    """
    if value is None:
        return None
    if value.units == unit:
        return value
    return value.to(unit)