# energy.gas-kwh
Turns the units a gas meter recorded into the kilowatt-hours a GB gas bill
charges for:
```
cubic metres = units (metric meter)
= units x 2.83 (imperial meter, reading in hundreds of cubic feet)
kWh = cubic metres x 1.02264 x calorific value (MJ/m³) / 3.6
```
1.02264 is the volume correction factor prescribed by the Gas (Calculation
of Thermal Energy) Regulations 1996 for a meter that is not temperature and
pressure corrected: it brings the metered volume to the standard conditions of
15 °C and 1013.25 mbar. 3.6 converts megajoules to kilowatt-hours. The
calorific value is the average for the gas delivered to the property over the
billing period, and is printed on the bill.
## Exactness and rounding
Everything is done in integers. The whole formula reduces to
```
watt-hours = hundredths of m³ x 4261 x CV in thousandths of MJ/m³ / 1,500,000
```
(102264 / 36,000,000 = 4261 / 1,500,000), computed exactly and rounded
**once, half-up, to the nearest watt-hour** (a thousandth of a kWh). A float
version of the same formula drifts in the last place and, for a large site,
overflows the exact range of a JavaScript number before it divides; here the
volume is split by the divisor first so every intermediate stays exact.
Suppliers round the kWh they print on a bill in different ways (whole kWh,
two decimal places). Round the watt-hours to the precision your bill shows;
the capability keeps three places so nothing is lost before that.
## Decisions
- **The constants are not dated data.** 1.02264, 3.6 and the 2.83 factor for
hundreds of cubic feet have been fixed since the Regulations were made and
are not rates that change by date. The calorific value, which does change
daily, is an argument.
- **2.83, not 2.8317.** 100 ft³ is 2.8316846592 m³ exactly, but the
conversion prescribed for billing, and used on every GB bill, is 2.83. Using
the exact factor would disagree with the supplier by about 0.06%.
- **Whole units.** The volume is the difference between two reads of the
black digits; the red digits are ignored when reading. Use
`energy.meter-advance` to get it from two reads across a rollover.
- **Bounds.** The volume is 0 to 10^12 units and the calorific value 1 to
100000 (100 MJ/m³; natural gas is kept between 38 and 41). A result that
would not be an exact integer in JavaScript is refused rather than rounded.
## Sources
- Office for Product Safety and Standards, "Gas meter readings and bill
calculation", https://www.gov.uk/guidance/gas-meter-readings-and-bill-calculation
(the 2.83 conversion, the calorific value, the 1.02264 factor and the
division by 3.6, all "as prescribed in the Gas (Calculation of Thermal
Energy) Regulations 1996").
- The Gas (Calculation of Thermal Energy) Regulations 1996, SI 1996/439,
https://www.legislation.gov.uk/uksi/1996/439