gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

Add Fuel Economy Units #63

Open wcomartin opened 8 years ago

wcomartin commented 8 years ago

MPG and L/100KM mpg and l/100km

~~Conversion base : 1 l/100km = 235.2145833 mpg ~~ Conversion base : 1 mpg = 235.2145833 l/100km

More information here. https://www.unitjuggler.com/convert-fuelconsumption-from-lper100km-to-mpgimperial.html?val=14

Edit: More Info here seems to be more complicated than i first thought, http://www.wikihow.com/Convert-MPG-to-Liters-per-100km

librilex commented 7 years ago

The unit mpg (miles per US gallon) can already be done by using the unit 'mi/gal'. To convert it to l/(100 km) is a bit tricky. You could use the unit 'cl/km' (centiliters per kilometer), which has the same meaning as l/(100 km):

Qty('35 mi/gal').to('cl/km') // outputs quantity of '6.72 cl/km' equaling 6.72 l/(100 km)

To add the fuel consumption pseudo unit 'l/100km' you would have to add the unit '100 km':

var UNITS = {
  // ... //
  /* fuel efficiency unit and length unit required for consumption unit l/100km */
  "<mpg>" : [["mpg"], 425143.7056332947, "fuel efficiency", ["<meter>"], ["<meter>","<meter>","<meter>"]],
  "<100km>": [["100km"], 100000, "length", ["<meter>"]],
  // ... //
};

You should add the unit kind for fuel efficiency, which basically is a reciprocal area (mpg = mi/gal ==> m/m³ = 1/m²), the unit signature would be -2.

Then you could use the conversion function to() as follows:

Qty('35 mpg').to('l/100km') // outputs quantity of about '6.7 l/100km'
Qty('5 l/100km').to('mpg') // outputs quantity of roughly '47 mpg'
bcowgill commented 4 years ago

Would be great if the above logic was wrapped up into a Qty.addUnit() function which set up the unit and added it to the relevant kinds lists...