I have a C# app and I need to convert between 3 different units (say for example: litres, gallons, and pints).
The app needs to know about certain volumes of liquid, say: 1 pint, 10 pints, 20 pints and 100 pints. I intend to do the calculations and hard code the values (not ideal but necessary),
I’m looking for a data structure that will allow me to easily convert from one unit to another.
Any suggestions?
Please note: I’m not actually using volumes of liquid, its just an example!
You can store a matrix of conversion factors where
You’d have (not accurate, but assuming there are two pints to a litre and 4 litres to a gallon)
Alternatively, you can decide that everything is converted to a base value (litres) before being converted to another type, then you just need the first line.
Wrap this in a method that takes a number of units and ‘from’ type and ‘two’ type for the conversion.
Hope this helps
EDIT: some code, as requested