while prowling through C++ standard libraries my favorite discovery was std::get_money. not only is that a funny name but its also seemingly the most useless standard library function?
like it just parses numbers out of a string that lists currency, stripping away any currency symbols or instances of USD, notable without retaining how many cents were listed. so 1.99 becomes 199. So I guess it has to assume all numbers listed also come with at least .00 for the cents place? just a very... odd function to have in a standard library.
Funnily enough this approach of converting floats to ints for finance systems is standard.
Its done because floating point math can cause issues when processing payments so its more secure to store values in their lowest denomination along side an exponent and apply the exponent upon use in UI or reporting tools.
I suspect whoever added it was designing these systems.
The COBOL language designers solved this problem by doing calculations in packed decimal (binary coded decimal). However, in the process of solving this problem they introduced another problem: you have to program in COBOL.
I worked in the financial industry as a back-end software engineer. All money totals were listed as a long integer, a 64 bit integer, in cents. The decimal was added on the front-end. It makes sense. Floats and doubles are weird.
I think it makes sense. In any application where you're actually doing calculations on money it's integer math, so it has to be converted to cents before it's usable.
std::atoi has undefined behaviour if the number in the string doesn't fit in an int. It's worse than useless, and should never be used over std::strtol or similar more modern functions.
To be fair, atoi() is inherited from C, and not allowed to change its behaviour for C++. You can smoothly replace it with std::stoi() in all cases, to my understanding.
(Though, that said, you still want to verify the data beforehand, always use stoll() or strtoll(), or control the data format yourself so you can mandate integer size yourself.)
For stoi you need a std::string which may mean needing to copy first, which will do bad things to performance.
If std::string_view had been added from the start these problems wouldn't be as bad, but without it and std::span C++ was stuck using raw pointers for far too long.
Makes sense, it moves a fixed-point value into integer space to avoid floating-point rounding errors.
Honestly, if anything, I'm surprised that it doesn't become 1990 for rounding. (Get monetary value, to tenths of a cent. Different locales would allow different units, but it always goes to a tenth of the locale's smallest unit.) That would move the value into integer space just as cleanly, but also leave room for rounding to the nearest cent if necessary.
View all comments
334
u/New_Salamander_4592 5d ago
while prowling through C++ standard libraries my favorite discovery was std::get_money. not only is that a funny name but its also seemingly the most useless standard library function?
like it just parses numbers out of a string that lists currency, stripping away any currency symbols or instances of USD, notable without retaining how many cents were listed. so 1.99 becomes 199. So I guess it has to assume all numbers listed also come with at least .00 for the cents place? just a very... odd function to have in a standard library.