I am trying to figure this out, the market data returns money values as a string that is 8 places after the digit long.
money = "124.19000540"
I need this to be 124.19, any idea how to achieve this?
std::stof(money) = 124.19000244
How to overcome this?
Floating point types are not good for holding money values. If you're content with rounding to the cent, and storing money as an integer number of cents (which is one of the simplest solutions), you could do this:
long numCents = static_cast<long>(100 * std::stof(money))
This will do "truncating" rounding, which always rounds down. If you'd like to do rounding "to the nearest cent", try:
long numCents = static_cast<long>(100 * std::stof(money) + 0.5)
As others mentioned, you may want to go for a fixed point or decimal library instead of something this simple.