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?
To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).
C++ string to float and double Conversion The easiest way to convert a string to a floating-point number is by using these C++11 functions: std::stof() - convert string to float. std::stod() - convert string to double. std::stold() - convert string to long double .
There is no functionality difference between string and std::string because they're the same type.
float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); Parses str interpreting its content as a floating-point number, which is returned as a value of type float. If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.
std::stof : It convert string into float. float stof ( const string& str, size_t* pos = 0 ); float stof ( const wstring& str, size_t* pos = 0 ); Parameters str : the string to convert pos : address of an integer to store the number of characters processed This parameter can also be a null pointer, in which case it is not used.
float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); Parameters : str : String object with the representation of a floating-point number. idx : Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
1 std::stod () : It convert string into double. Syntax: double stod ( const std::string& str, std::size_t* pos = 0 ); double stod ( const std::wstring& str, std::size_t* pos = 0 ... 2 std::stof : It convert string into float. ... 3 std::stold : It convert string into long double. ...
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.
You can use setprecision
#include <iostream> //for std::fixed
#include <sstream> //for std::stringstream
#include <iomanip> //for std::setprecision
int main()
{
std::string money;
std::cout << "Enter amount: ";
getline(std::cin, money);
std::stringstream out;
out << std::fixed << std::setprecision(2) << std::stof(money);
float fmoney = std::stof(out.str());
std::cout << "Result: " << fmoney << std::endl;
return 0;
}
Execution Output:
Enter amount: 124.19000540
Result: 124.19
Instead of std::stof()
, which converts the string to a float,
you should use std::stod()
, which converts it to a double (with higher precision).
But as others also said, even this way you often cannot get exactly the same digits, due to how floating point numbers are represented. When printing through std::cout
, you can use << std::setprecision(<precision>)
to control the number of digits to print.
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