unit-converter

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unit Converter

单位转换器

Convert between units across multiple physical dimensions.
在多类物理维度的单位之间进行转换。

Features

功能特性

  • Multiple Categories: Length, mass, temperature, time, volume, etc.
  • Compound Units: Speed, density, pressure
  • Precision Control: Configurable decimal places
  • Batch Conversion: Convert lists of values
  • Formula Display: Show conversion formulas
  • 多类别支持:长度、质量、温度、时间、体积等。
  • 复合单位:速度、密度、压力
  • 精度控制:可配置小数位数
  • 批量转换:转换数值列表
  • 公式展示:显示转换公式

Quick Start

快速开始

python
from unit_converter import UnitConverter

converter = UnitConverter()
python
from unit_converter import UnitConverter

converter = UnitConverter()

Simple conversion

简单转换

result = converter.convert(100, "km", "miles") print(f"100 km = {result:.2f} miles")
result = converter.convert(100, "km", "miles") print(f"100 km = {result:.2f} miles")

With full details

带详细信息的转换

result = converter.convert_with_details(72, "fahrenheit", "celsius") print(result)
undefined
result = converter.convert_with_details(72, "fahrenheit", "celsius") print(result)
undefined

CLI Usage

CLI 使用方法

bash
undefined
bash
undefined

Basic conversion

基础转换

python unit_converter.py 100 km miles
python unit_converter.py 100 km miles

Temperature

温度转换

python unit_converter.py 98.6 fahrenheit celsius
python unit_converter.py 98.6 fahrenheit celsius

With precision

带精度设置

python unit_converter.py 1.5 kg lbs --precision 4
python unit_converter.py 1.5 kg lbs --precision 4

List supported units

列出支持的单位

python unit_converter.py --list
python unit_converter.py --list

List units in category

列出指定类别下的单位

python unit_converter.py --list length
python unit_converter.py --list length

Show formula

显示转换公式

python unit_converter.py 100 cm inches --formula
undefined
python unit_converter.py 100 cm inches --formula
undefined

API Reference

API 参考

UnitConverter Class

UnitConverter 类

python
class UnitConverter:
    def __init__(self)

    # Conversion
    def convert(self, value: float, from_unit: str, to_unit: str) -> float
    def convert_with_details(self, value: float, from_unit: str, to_unit: str) -> dict
    def batch_convert(self, values: list, from_unit: str, to_unit: str) -> list

    # Information
    def list_categories(self) -> list
    def list_units(self, category: str = None) -> dict
    def get_formula(self, from_unit: str, to_unit: str) -> str
    def find_unit(self, query: str) -> list
python
class UnitConverter:
    def __init__(self)

    # 转换方法
    def convert(self, value: float, from_unit: str, to_unit: str) -> float
    def convert_with_details(self, value: float, from_unit: str, to_unit: str) -> dict
    def batch_convert(self, values: list, from_unit: str, to_unit: str) -> list

    # 信息查询方法
    def list_categories(self) -> list
    def list_units(self, category: str = None) -> dict
    def get_formula(self, from_unit: str, to_unit: str) -> str
    def find_unit(self, query: str) -> list

Supported Categories

支持的类别

Length

长度

UnitAliases
meterm
kilometerkm
centimetercm
millimetermm
inchin
footft
yardyd
milemi
nautical_milenm
单位别名
meterm
kilometerkm
centimetercm
millimetermm
inchin
footft
yardyd
milemi
nautical_milenm

Mass

质量

UnitAliases
kilogramkg
gramg
milligrammg
poundlb, lbs
ounceoz
ton
metric_tontonne
单位别名
kilogramkg
gramg
milligrammg
poundlb, lbs
ounceoz
ton
metric_tontonne

Temperature

温度

UnitAliases
celsiusc
fahrenheitf
kelvink
单位别名
celsiusc
fahrenheitf
kelvink

Time

时间

UnitAliases
seconds, sec
minutemin
hourh, hr
dayd
weekwk
monthmo
yearyr
单位别名
seconds, sec
minutemin
hourh, hr
dayd
weekwk
monthmo
yearyr

Volume

体积

UnitAliases
literl
milliliterml
gallongal
quartqt
pintpt
cup
fluid_ouncefl_oz
cubic_meterm3
单位别名
literl
milliliterml
gallongal
quartqt
pintpt
cup
fluid_ouncefl_oz
cubic_meterm3

Area

面积

UnitAliases
square_meterm2, sqm
square_kilometerkm2
square_footsqft, ft2
acre
hectareha
单位别名
square_meterm2, sqm
square_kilometerkm2
square_footsqft, ft2
acre
hectareha

Speed

速度

UnitAliases
meters_per_secondm/s, mps
kilometers_per_hourkm/h, kph
miles_per_hourmph
knotskt
单位别名
meters_per_secondm/s, mps
kilometers_per_hourkm/h, kph
miles_per_hourmph
knotskt

Digital Storage

数字存储

UnitAliases
byteb
kilobytekb
megabytemb
gigabytegb
terabytetb
单位别名
byteb
kilobytekb
megabytemb
gigabytegb
terabytetb

Energy

能量

UnitAliases
joulej
kilojoulekj
caloriecal
kilocaloriekcal
watt_hourwh
kilowatt_hourkwh
单位别名
joulej
kilojoulekj
caloriecal
kilocaloriekcal
watt_hourwh
kilowatt_hourkwh

Example Workflows

示例工作流

Batch Conversion

批量转换

python
converter = UnitConverter()
weights_kg = [50, 75, 100, 125]
weights_lbs = converter.batch_convert(weights_kg, "kg", "lbs")
for kg, lbs in zip(weights_kg, weights_lbs):
    print(f"{kg} kg = {lbs:.1f} lbs")
python
converter = UnitConverter()
weights_kg = [50, 75, 100, 125]
weights_lbs = converter.batch_convert(weights_kg, "kg", "lbs")
for kg, lbs in zip(weights_kg, weights_lbs):
    print(f"{kg} kg = {lbs:.1f} lbs")

Find Compatible Units

查找兼容单位

python
converter = UnitConverter()
python
converter = UnitConverter()

Search for units

搜索单位

matches = converter.find_unit("meter")
matches = converter.find_unit("meter")

Returns: ['meter', 'kilometer', 'centimeter', ...]

返回结果: ['meter', 'kilometer', 'centimeter', ...]

undefined
undefined

Get Conversion Formula

获取转换公式

python
converter = UnitConverter()
formula = converter.get_formula("celsius", "fahrenheit")
print(formula)  # "F = (C × 9/5) + 32"
python
converter = UnitConverter()
formula = converter.get_formula("celsius", "fahrenheit")
print(formula)  # "F = (C × 9/5) + 32"

Output Format

输出格式

convert_with_details()

convert_with_details()

python
{
    "value": 100,
    "from_unit": "km",
    "to_unit": "miles",
    "result": 62.1371,
    "formula": "miles = km × 0.621371",
    "category": "length"
}
python
{
    "value": 100,
    "from_unit": "km",
    "to_unit": "miles",
    "result": 62.1371,
    "formula": "miles = km × 0.621371",
    "category": "length"
}

Dependencies

依赖项

No external dependencies - uses Python standard library.
无需外部依赖 - 使用Python标准库。