How to make a totalizer?

A better solution would be to store your values in a database, then use the database to do your calculations. A couple of blasts from the past: