Is it safe to store prices in double variable in C++? In C# and Java you can use Decimal. (They also say that storing prices in double is very bad idea). What should I do in C++?
If the price range is suitable (probably it is), you can use long long
with an implied dot before the last two digits. So, 12000 will mean 120.00. It's called "fixed point" arithmetic. But you need to take care of multiplications of two currency values (why would you need it anyway).
You can have a look at Intel® Decimal Floating-Point Math Library for storing price. Also you can check the Boost.Multiprecision library which can be very useful, you can use decimal based floating point template class called cpp_dec_float
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With