Thing to keep in mind with determining state-of-charge of batteries is that their voltage is only a rough indication of that, and particularly when they're under load their internal resistance changes their voltage output, which misleads you into thinking they're at a lower SoC than they actually are. This is particularly the case with Lithium chemistry batteries, because their discharge-vs-time curve is comparatively flat (not perfectly flat, just flatter than other chemistries) for most of their capacity (except the start & end).
"Professional" battery SoC calculation is done by integrating the area under the current-vs-time curve, essentially to count how many coulombs of energy is going into or out of the battery, & comparing that to either (a) the theoretical/designed coulomb capacity of the battery, or (b) keeping track over long periods of time how many coulombs you get out of a 'full charge', which is preferable because this drops as the battery ages.
Encapsulated batteries like are in laptops & cell-phones have these chips built into the case of the battery (along with the battery protection module), and the host CPU can interrogate the battery to find out its SoC as a neat & fairly accurate %age.
Allowing your gadget to operate all the way down until it turns itself off, once in a while, is a good way letting the 'gas-guage' chip update its total-coulomb-capacity measurement.
As @Matt Young said, there are many 'gas gauge' battery management chips out there for doing this.
related:
http://jeelabs.org/2012/11/26/watts-amps-coulombs/
Why are batteries measured in ampere-hours but electricity usage measured in kilowatt-hours?