The 13th entry of "to" in OALD：
used to show a quantity or rate
There are 2.54 centimetres to an inch.
This car does 30 miles to the gallon.
I'm wondering why the second example is used with the, not a.
English Language Learners Stack Exchange is a question and answer site for speakers of other languages learning English. It only takes a minute to sign up.Sign up to join this community
"<This car> does 30 miles to the gallon" is an informal idiom. In this case, "the gallon" is not a specific gallon.
"<This car> gets 30 miles per gallon" is a more formal idiom with the same meaning.
"Given a gallon of gas, <this car> will go 30 miles" is a less common way of saying the same thing. In this case, the choice between "a" and "the" is based on whether you have a specific gallon of gas in mind. If someone just put a gallon of gas in the car, you would say "the gallon". If you are speaking about the car's gas mileage in general, you would say "a gallon".
There doesn't seem to be a consensus by usage, except that the is preferred to a.
Source: Google Books Ngram
Either one is acceptable, but searching for usage in context shows a different pattern. to the gallon is primarily associated with the ratio of miles to gallons:
The car does 30 miles to the gallon.
But to a gallon is primarily associated with addition or additives:
This adds 40 cents to a gallon of gas.
When in doubt, do what I did. Plug the concept into a search engine and see the context.