system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: How do electronics know what battery percentage they have?
gpt
In short there are two ways, sometimes combined.

1) Measure the battery voltage and compare this against a graph that you obtained through calibration. This will tell you at what voltage, what the battery SOC (State-of-Charge) is. This will not be a linear relation. (being halfway between two extremes does not imply being half charged). Generally this is the *least* accurate way to measure, but it isn't affected by long-term errors.

2) You measure the current going in and out of the battery. You know how much energy a battery will hold if full. Current can describe how much energy escapes the battery every second. Therefore if you measure half the energy has left, the battery is half full. This method is affected by errors over long periods of time, but is much more accurate on shorter periods.