A question about the laptop’s battery icon

Hello! I was wondering how does a laptop decide what portion of the battery is still left to run the laptop. Lets say at a certain time instance it says 63% remaining, how exactly does the machinery of the laptop calculate the remaining battery. Is there some sort of formula? And how does it calculate the remaining time like right now my laptop says 28 min(18%) remaining.
