Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string to float (via std::stof) precision

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?

like image 819
PeeS Avatar asked Jul 14 '15 19:07

PeeS


People also ask

How do you set the precision of a float in C++?

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).

How do you convert a string to a float in C++?

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 .

Is string the same as std :: string?

There is no functionality difference between string and std::string because they're the same type.

How does float Stof work in C++?

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.

How do you convert a string to a float?

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.

What are the parameters of float Stof?

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.

What is the difference between Stod and Stof in C++?

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. ...


Video Answer


3 Answers

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.

like image 91
brenns10 Avatar answered Nov 03 '22 15:11

brenns10


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
like image 41
Lejuanjowski Avatar answered Nov 03 '22 16:11

Lejuanjowski


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.

like image 24
jciloa Avatar answered Nov 03 '22 17:11

jciloa