Skip to content

General Converter — v3.0.0#34

Open
aranapatona wants to merge 3 commits intodeefrawley:mainfrom
aranapatona:main
Open

General Converter — v3.0.0#34
aranapatona wants to merge 3 commits intodeefrawley:mainfrom
aranapatona:main

Conversation

@aranapatona
Copy link

General Converter — v3.0.0

A significant rewrite focused on accuracy, extensibility, and usefulness for technical and scientific work. This release adds 10 new categories, 100+ new units, a cleaner internal architecture, clipboard support, and matching icons for every category.


⚡ What's New at a Glance

v2.1.0 v3.0.0
Categories 9 19
Total units ~70 ~175
Conversion engine eval() on formula strings Pure arithmetic (factor/offset)
Clipboard copy ✓ Enter copies result
Category icons 9 19 (all categories covered)
Bug-free factors Some errors (see below) Corrected, exact where possible

🆕 New Categories

Force

N · mN · kN · MN · dyn · kgf · lbf · pdl

Newton, millinewton, kilonewton, meganewton, dyne, kilogram-force, pound-force, poundal.

Power

W · mW · kW · MW · GW · hp · hpmet · BTU/hr · kcal/hr · VA

Watt through gigawatt, mechanical and metric horsepower, BTU/hr, kilocalorie/hr, volt-ampere.

Torque

Nm · kNm · mNm · lbf_ft · lbf_in · kgfm · ozf_in · dyn_cm

Newton-metre through millinewton-metre, pound-foot, pound-inch, kilogram-force metre, ounce-force inch, dyne-centimetre.

Frequency

Hz · kHz · MHz · GHz · THz · rpm · rps · rad/s

Hertz through terahertz, RPM, revolutions per second, radians per second.

Angle

deg · rad · grad · arcmin · arcsec · rev · mrad

Degree, radian, gradian, arcminute, arcsecond, revolution, milliradian.

Amount of Substance

mol · mmol · µmol · nmol · pmol · kmol

Mole through picomole and kilomole. Essential for chemistry and biochemistry.

Radioactivity

Bq · kBq · MBq · GBq · TBq · Ci · mCi · µCi · nCi · dpm

Becquerel through terabecquerel, curie through nanocurie, disintegrations per minute.

Radiation Dose (absorbed)

Gy · mGy · µGy · cGy · rad · mrad

Gray, milligray, microgray, centigray, rad, millirad.

Dose Equivalent

Sv · mSv · µSv · rem · mrem

Sievert, millisievert, microsievert, rem, millirem.

Density

kg/m3 · g/cm3 · g/mL · kg/L · g/L · mg/L · lb/ft3 · lb/in3 · lb/gal

SI and imperial density units, including g/mL and mg/L common in chemistry and lab work.

Dynamic Viscosity

Pa_s · mPa_s · cP · P · lb/ft_s

Pascal-second, millipascal-second, centipoise, poise, pound per foot-second.

Kinematic Viscosity

m2/s · mm2/s · cSt · St · ft2/s

Square metre per second, square millimetre per second, centistokes, stokes, square foot per second.


🔧 Expanded Existing Categories

Distance

Added: µm (micrometre), nm (nanometre), Å (ångström), nmi (nautical mile), ly (light-year), au (astronomical unit), pc (parsec)

Area

Added: sqmm (square millimetre)

Volume

Added: cl (centilitre), dl (decilitre)

Weight / Mass

Added: mg (milligram), µg (microgram), ng (nanogram), Da (dalton), u (atomic mass unit)

Temperature

Added: r (Rankine) — used in thermodynamics and aerospace engineering

Speed

Added: ft/s (feet per second), mach (Mach number at sea level, 15°C), c (speed of light)

Energy

Added: mJ (millijoule), MWh (megawatt-hour), therm, eV / keV / MeV / GeV (electronvolts), erg

Pressure

Rebuilt with Pa as base (was bar). Added: hPa (hectopascal), MPa, GPa, ksi, mmHg, Torr, inHg


🐛 Bug Fixes in Existing Units

These were silent wrong-answer bugs in v2.1.0:

Category Unit v2.1.0 factor (wrong) v3.0.0 factor (correct)
Volume mm3 ÷ 0.1 → ×10 ml ÷ 1000 → 0.001 ml ✓
Area sqkm ÷ 1,000,000 → 0.000001 m² × 1,000,000 → 1,000,000 m² ✓
Volume g Stray gram entry copied from Weight Removed ✓
Pressure kPa Implied 0.001 bar/kPa 0.01 bar/kPa ✓

Spelling fix: FarenheitFahrenheit

Capitalisation fix: GjGJ (gigajoule)


✨ Clipboard Support

Pressing Enter on a conversion result now copies the converted value to the clipboard. The value is copied without thousands separators so it pastes cleanly into calculations, spreadsheets, or code.

10 km/h m/s   →   Enter copies: 2.778

🏗️ Internal Architecture

Why the format changed

The old units.py stored conversions as a pair of formula strings that were eval()'d at runtime:

# Old format
["km", _("kilometre"), _("kilometres"), "x / 0.001", "x * 0.001"]
#                                        ^ TO base     ^ FROM base
#                                        eval'd at runtime — slow, fragile, and a security risk

This had several problems:

  • The two formulas must always be exact inverses of each other — easy to get wrong by hand, and four of them were wrong in v2.1.0 (see bug fixes above)
  • The factor meaning is buried: "x / 0.001" is less readable than 1_000 (1 km = 1000 m)
  • eval() on user-facing data is a security surface
  • String manipulation on every conversion is needlessly slow

New format — linear units

Each unit is a 4-tuple: (abbreviation, singular, plural, factor)

The factor is the answer to: "how many base units make up one of this unit?"

# Full category structure
_("Distance"): {
    "base": "m",           # abbreviation of the base unit
    "units": [
        #  abbr    singular       plural         factor
        ("m",   _("metre"),    _("metres"),    1),        # base — always 1
        ("cm",  _("centimetre"), _("centimetres"), 0.01), # 1 cm = 0.01 m
        ("km",  _("kilometre"), _("kilometres"), 1_000),  # 1 km = 1000 m
        ("in",  _("inch"),     _("inches"),    0.0254),   # 1 in = 0.0254 m (exact)
        ("mi",  _("mile"),     _("miles"),     1_609.344),# 1 mi = 1609.344 m (exact)
    ]
}

The engine converts any pair using only two multiplications — no strings, no eval:

to_base   = value × src_factor
result    = to_base ÷ dst_factor

New format — affine units (Temperature)

Units that don't convert via a simple ratio (Temperature, Rankine) use a 5-tuple with an extra offset field:

_("Temperature"): {
    "base": "c",
    "units": [
        #  abbr   singular       plural         factor   offset
        ("c",  _("Celsius"),    _("Celsius"),    1,       0),
        ("f",  _("Fahrenheit"), _("Fahrenheit"), 1/1.8,  -32),    # °F → °C: (x - 32) / 1.8
        ("k",  _("Kelvin"),     _("Kelvin"),     1,      -273.15), # K  → °C: x - 273.15
        ("r",  _("Rankine"),    _("Rankine"),    1/1.8,  -491.67), # °R → °C: (x - 491.67) / 1.8
    ]
}

The engine detects the 5-tuple and applies the affine formula:

to_base = (value + offset) × factor
result  = (to_base ÷ dst_factor) − dst_offset

The offset encodes what you add to the raw value before scaling. For Fahrenheit: add −32 (i.e. subtract 32), then divide by 1.8. This matches the standard formula exactly and handles round-trips correctly.


Adding your own units

Add a unit to an existing category — append a tuple to the "units" list. The factor is simply "how many base units equal one of this unit":

# Weight base is grams — 1 Chinese jin = 500 g
("jin", _("Chinese jin"), _("Chinese jin"), 500)

Add a brand new category:

_("My Category"): {
    "base": "mybase",
    "units": [
        ("mybase", _("base unit"),  _("base units"),  1),    # base always has factor 1
        ("other",  _("other unit"), _("other units"), 42.0), # 1 other = 42 base units
    ]
}

Then add a corresponding assets/My Category.ico (128×128 RGBA, transparent background).


📋 Full Unit Reference

Units are case-sensitive. Base unit listed first for each category.

Distance (base: m)

Code Unit
m metre
dm decimetre
cm centimetre
mm millimetre
µm micrometre
nm nanometre
km kilometre
in inch
ft foot
yd yard
mi mile
nmi nautical mile
ly light-year
au astronomical unit
pc parsec
Å ångström

Area (base: sqm)

Code Unit
sqm square metre
sqcm square centimetre
sqmm square millimetre
sqkm square kilometre
sqin square inch
sqft square foot
sqyd square yard
sqmi square mile
ac acre
h hectare

Volume (base: ml)

Code Unit
ml millilitre
cl centilitre
dl decilitre
l litre
decal decalitre
m3 cubic metre
cm3 cubic centimetre
mm3 cubic millimetre
dm3 cubic decimetre
in3 cubic inch
ft3 cubic foot
pt / ptimp pint US / Imperial
qt / qtimp quart US / Imperial
cup / cupimp cup US / Imperial
tbsp / tbspimp tablespoon US / Imperial
tsp / tspimp teaspoon US / Imperial
gal / galimp gallon US / Imperial
floz / flozimp fluid ounce US / Imperial
buuk / buus bushel UK / US

Weight / Mass (base: g)

Code Unit
g gram
mg milligram
µg microgram
ng nanogram
kg kilogram
t tonne
lb pound
oz ounce
st stone
ton US ton
tonimp Imperial ton
Da dalton
u atomic mass unit

Temperature (base: c)

Code Unit
c Celsius
f Fahrenheit
k Kelvin
r Rankine

Speed (base: m/s)

Code Unit
m/s metres per second
km/h kilometres per hour
mp/h miles per hour
ft/s feet per second
kt knot
mach Mach (sea level, 15°C)
c speed of light

Force (base: N)

Code Unit
N newton
mN millinewton
kN kilonewton
MN meganewton
dyn dyne
kgf kilogram-force
lbf pound-force
pdl poundal

Pressure (base: Pa)

Code Unit
Pa pascal
hPa hectopascal
kPa kilopascal
MPa megapascal
GPa gigapascal
bar bar
mbar millibar
atm atmosphere
mmHg millimetre of mercury
torr torr
psi psi
ksi ksi
inHg inch of mercury

Energy (base: J)

Code Unit
J joule
mJ millijoule
kJ kilojoule
MJ megajoule
GJ gigajoule
cal calorie (IT)
kcal kilocalorie
kWh kilowatt-hour
MWh megawatt-hour
BTU British thermal unit
therm therm
eV electronvolt
keV kiloelectronvolt
MeV megaelectronvolt
GeV gigaelectronvolt
erg erg

Power (base: W)

Code Unit
W watt
mW milliwatt
kW kilowatt
MW megawatt
GW gigawatt
hp horsepower (mechanical)
hpmet horsepower (metric)
BTU/hr BTU per hour
kcal/hr kilocalorie per hour
VA volt-ampere

Torque (base: Nm)

Code Unit
Nm newton-metre
kNm kilonewton-metre
mNm millinewton-metre
lbf_ft pound-foot
lbf_in pound-inch
kgfm kilogram-force metre
ozf_in ounce-force inch
dyn_cm dyne-centimetre

Frequency (base: Hz)

Code Unit
Hz hertz
kHz kilohertz
MHz megahertz
GHz gigahertz
THz terahertz
rpm RPM
rps revolutions per second
rad/s radians per second

Angle (base: deg)

Code Unit
deg degree
rad radian
grad gradian
arcmin arcminute
arcsec arcsecond
rev revolution
mrad milliradian

Amount of Substance (base: mol)

Code Unit
mol mole
mmol millimole
µmol micromole
nmol nanomole
pmol picomole
kmol kilomole

Radioactivity (base: Bq)

Code Unit
Bq becquerel
kBq kilobecquerel
MBq megabecquerel
GBq gigabecquerel
TBq terabecquerel
Ci curie
mCi millicurie
µCi microcurie
nCi nanocurie
dpm disintegrations per minute

Radiation Dose (base: Gy)

Code Unit
Gy gray
mGy milligray
µGy microgray
cGy centigray
rad rad
mrad millirad

Dose Equivalent (base: Sv)

Code Unit
Sv sievert
mSv millisievert
µSv microsievert
rem rem
mrem millirem

Density (base: kg/m3)

Code Unit
kg/m3 kilogram per cubic metre
g/cm3 gram per cubic centimetre
g/mL gram per millilitre
kg/L kilogram per litre
g/L gram per litre
mg/L milligram per litre
lb/ft3 pound per cubic foot
lb/in3 pound per cubic inch
lb/gal pound per US gallon

Dynamic Viscosity (base: Pa_s)

Code Unit
Pa_s pascal-second
mPa_s millipascal-second
cP centipoise
P poise
lb/ft_s pound per foot-second

Kinematic Viscosity (base: m2/s)

Code Unit
m2/s square metre per second
mm2/s square millimetre per second
cSt centistokes
St stokes
ft2/s square foot per second

Data (base: B)

Code Unit
B byte
KB MB GB TB PB kilobyte → petabyte (SI)
b Kb Mb Gb Tb Pb bit → petabit (SI)
KiB MiB GiB TiB PiB kibibyte → pebibyte (IEC)
Kib Mib Gib Tib Pib kibibit → pebibit (IEC)

🙏 Credits

Original plugin by @deefrawley.
v3.0.0 expansion and refactor contributed by the community.

A significant rewrite focused on accuracy, extensibility, and usefulness for technical and scientific work. This release adds 10 new categories, 100+ new units, a cleaner internal architecture, clipboard support, and matching icons for every category.
A  significant rewrite focused on accuracy, extensibility, and usefulness for technical and scientific work. This release adds 10 new categories, 100+ new units, a cleaner internal architecture, clipboard support, and matching icons for every category.
@deefrawley
Copy link
Owner

A very comprehensive refactor, thank you very much for the work that has gone into this. Can you update the README for the repo to update it with the information above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants