Battery Capacity Converter
Convert battery capacity between mAh, Ah, Wh, and kWh with voltage selection.
mAh (Milliampere-hour)
5,000
Ah (Ampere-hour)
5
Wh (Watt-hour)
18.5
kWh (Kilowatt-hour)
0.0185
BatteryCapacityConverter.formulaNote
Wh = (mAh × V) ÷ 1000
Ah = mAh ÷ 1000
BatteryCapacityConverter.voltageNote
Часто задаваемые вопросы
Реализация кода
def convert_battery_capacity(value: float, from_unit: str, to_unit: str,
voltage: float = 3.7) -> float:
"""
Convert battery capacity between mAh, Ah, Wh, kWh.
voltage is required for mAh/Ah <-> Wh/kWh conversions.
"""
# Convert to milliwatt-hours as base
if from_unit == "mAh":
base_mwh = value * voltage
elif from_unit == "Ah":
base_mwh = value * 1000 * voltage
elif from_unit == "Wh":
base_mwh = value * 1000
elif from_unit == "kWh":
base_mwh = value * 1_000_000
else:
raise ValueError(f"Unknown unit: {from_unit}")
if to_unit == "mAh":
return base_mwh / voltage
elif to_unit == "Ah":
return base_mwh / (1000 * voltage)
elif to_unit == "Wh":
return base_mwh / 1000
elif to_unit == "kWh":
return base_mwh / 1_000_000
else:
raise ValueError(f"Unknown unit: {to_unit}")
# Examples
print(f"5000 mAh @ 3.7V = {convert_battery_capacity(5000, 'mAh', 'Wh'):.2f} Wh")
print(f"18.5 Wh = {convert_battery_capacity(18.5, 'Wh', 'mAh'):.0f} mAh @ 3.7V")
print(f"100 Wh = {convert_battery_capacity(100, 'Wh', 'kWh'):.4f} kWh")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.