unit-converter
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnit 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)
undefinedresult = converter.convert_with_details(72, "fahrenheit", "celsius")
print(result)
undefinedCLI Usage
CLI 使用方法
bash
undefinedbash
undefinedBasic 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
undefinedpython unit_converter.py 100 cm inches --formula
undefinedAPI 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) -> listpython
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) -> listSupported Categories
支持的类别
Length
长度
| Unit | Aliases |
|---|---|
| meter | m |
| kilometer | km |
| centimeter | cm |
| millimeter | mm |
| inch | in |
| foot | ft |
| yard | yd |
| mile | mi |
| nautical_mile | nm |
| 单位 | 别名 |
|---|---|
| meter | m |
| kilometer | km |
| centimeter | cm |
| millimeter | mm |
| inch | in |
| foot | ft |
| yard | yd |
| mile | mi |
| nautical_mile | nm |
Mass
质量
| Unit | Aliases |
|---|---|
| kilogram | kg |
| gram | g |
| milligram | mg |
| pound | lb, lbs |
| ounce | oz |
| ton | |
| metric_ton | tonne |
| 单位 | 别名 |
|---|---|
| kilogram | kg |
| gram | g |
| milligram | mg |
| pound | lb, lbs |
| ounce | oz |
| ton | |
| metric_ton | tonne |
Temperature
温度
| Unit | Aliases |
|---|---|
| celsius | c |
| fahrenheit | f |
| kelvin | k |
| 单位 | 别名 |
|---|---|
| celsius | c |
| fahrenheit | f |
| kelvin | k |
Time
时间
| Unit | Aliases |
|---|---|
| second | s, sec |
| minute | min |
| hour | h, hr |
| day | d |
| week | wk |
| month | mo |
| year | yr |
| 单位 | 别名 |
|---|---|
| second | s, sec |
| minute | min |
| hour | h, hr |
| day | d |
| week | wk |
| month | mo |
| year | yr |
Volume
体积
| Unit | Aliases |
|---|---|
| liter | l |
| milliliter | ml |
| gallon | gal |
| quart | qt |
| pint | pt |
| cup | |
| fluid_ounce | fl_oz |
| cubic_meter | m3 |
| 单位 | 别名 |
|---|---|
| liter | l |
| milliliter | ml |
| gallon | gal |
| quart | qt |
| pint | pt |
| cup | |
| fluid_ounce | fl_oz |
| cubic_meter | m3 |
Area
面积
| Unit | Aliases |
|---|---|
| square_meter | m2, sqm |
| square_kilometer | km2 |
| square_foot | sqft, ft2 |
| acre | |
| hectare | ha |
| 单位 | 别名 |
|---|---|
| square_meter | m2, sqm |
| square_kilometer | km2 |
| square_foot | sqft, ft2 |
| acre | |
| hectare | ha |
Speed
速度
| Unit | Aliases |
|---|---|
| meters_per_second | m/s, mps |
| kilometers_per_hour | km/h, kph |
| miles_per_hour | mph |
| knots | kt |
| 单位 | 别名 |
|---|---|
| meters_per_second | m/s, mps |
| kilometers_per_hour | km/h, kph |
| miles_per_hour | mph |
| knots | kt |
Digital Storage
数字存储
| Unit | Aliases |
|---|---|
| byte | b |
| kilobyte | kb |
| megabyte | mb |
| gigabyte | gb |
| terabyte | tb |
| 单位 | 别名 |
|---|---|
| byte | b |
| kilobyte | kb |
| megabyte | mb |
| gigabyte | gb |
| terabyte | tb |
Energy
能量
| Unit | Aliases |
|---|---|
| joule | j |
| kilojoule | kj |
| calorie | cal |
| kilocalorie | kcal |
| watt_hour | wh |
| kilowatt_hour | kwh |
| 单位 | 别名 |
|---|---|
| joule | j |
| kilojoule | kj |
| calorie | cal |
| kilocalorie | kcal |
| watt_hour | wh |
| kilowatt_hour | kwh |
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', ...]
undefinedundefinedGet 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标准库。