0

I have a vector string with decimal value I want to convert it to integer like that 67.35 ----> 6735 atoi is not working ?

int n;   
std::vector<std::string> value;   
value= "67.35"

I want n to be 6735 I know I can write a long code to do that but I am asking if that is already done.

Undefined Behaviour
  • 729
  • 2
  • 8
  • 27
XYZ
  • 164
  • 1
  • 1
  • 13

2 Answers2

2

Is there any chance this might be your solution? Hope this helps.

#include <boost/lexical_cast.hpp>
#include <string>

int main() 
{    
    std::string sf = "42.2";     
    std::string si = "42";    
    float f = boost::lexical_cast<float>(sf); //f is 42.2    
    int i = boost::lexical_cast<int>(si);  //i is 42 
}

You can check these also:

How to convert a number to string and vice versa in C++

How do I convert vector of strings into vector of integers in C++?


"Anyway, the real answer is to use C++'s stof and stoi. The dupe is very thorough." (by "Lightness Races in Orbit" in comments)

Links: stof stoi

K. Furkan BEK
  • 233
  • 1
  • 2
  • 9
  • Using boost to convert a string to a number seems slightly overkill. This is a basic C/C++ feature. – pie3636 Jul 29 '16 at 09:57
  • @pie3636: What is "C/C++"? Actually, lexical_cast is much more flexible and is able to optimise where your use of antiquated `atoi` from the C standard library is not. You can also use it in generic code. This is not overkill; it is idiomatic and commonplace in C++. – Lightness Races in Orbit Jul 29 '16 at 09:59
  • C/C++ was meant to say that the `atoi()` and `atof()` functions are a part of the standard C library, and therefore happen to exist in C++ too. OP's question leads me to believe they don't have much experience with C++ or possibly programming in general, so my first answer would certainly not be to install a new library, which might throw them off even more; sure, performance is important, but here it's basic functionality OP is asking for. – pie3636 Jul 29 '16 at 10:03
  • @pie3636: It is much more basic to use lexical_cast than it is to dabble with the C library and all its foibles. `yum install boost-devel` done. – Lightness Races in Orbit Jul 29 '16 at 10:06
  • Anyway, the _real_ answer is to use C++'s `stof` and `stoi`. The dupe is very thorough. – Lightness Races in Orbit Jul 29 '16 at 10:06
  • Not everyone is programming under Linux :) – pie3636 Jul 29 '16 at 10:07
  • I updated the answer according to your contributions. Thank you all. – K. Furkan BEK Jul 29 '16 at 10:31
0

You don't need a vector if you're only going to store one value. The most straightforward way to do it would be:

#include <string> // This needs to be in your headers

float n;
std::string value = "47.65";
n = stof(value) // n is 47.65. Note that stof is needed instead of stoi for floats

If you need several values, this is how you would do it:

#include <string>

std::vector<std::string> values;
values.push_back("47.65");
values.push_back("12.34");
//...

std::vector<float> floatValues;
for (int i = 0; i <= values.size(); i++) {
    floatValues.push_back(stof(values[i]));
    // intValues is a vector containing 47.65 and 12.34
}

If you're looking to remove the decimal places in your strings before parsing them, you can use the following code:

#include <algorithm> // This also needs to be in your headers
#include <string>

int n;
std::string value = "47.65";
std::replace(value.begin(), value.end(), '.', '')
n = stoi(value) // n is 4765. This time we're using atoi since we have an int
pie3636
  • 795
  • 17
  • 31