4 ms·
What if you need to calculate the percentage difference between two monetary values? Ints wont cut it there.
by nicky0 9y ago
What if you need to calculate the percentage difference between two monetary values?
Ints wont cut it there.
- dragonwriter 9y agoSure they will. To truncated whole percents, (N2-N1)×100÷N1, using integer ops, gets you that, to whole percents rounded half up, ((N2-N1)×100+50)÷N1 does it. For additional decimal places, increase the fixed multiplier (and added factor for round-half-up) by the appropriate power of 10. Even if you are dealing in units of hundreds of billions of dollars tracked to mils, this gets you up to, I think, a hundred-thousandth of a percent with int64.
- nicky0 9y agoBut with floats you just use a divide operation.
- dragonwriter 9y ago“Won’t cut it” and “will require 1-2 more operations” are very much not the same thing.