1

"35.28" is stored as a char*. I need to turn it into an integer (35280).

I want to avoid floats. How can I do this?

Smurf64
  • 337
  • 1
  • 6
  • 15

5 Answers5

4

Minimal basic code:

std::string s = "35.28";
s.erase(std::remove(s.begin(), s.end(), '.'), s.end()); //removing the dot
std::stringstream ss(s);
int value;
ss >> value;
value *= 10;
std::cout << value;

Output:

35280

Online demo : http://ideone.com/apRNP

That is the basic idea. You can work on the above code to make it more flexible so that it can be used for other numbers as well.


EDIT:

Here is one flexible solution:

int Convert(std::string s, int multiplier) 
{
   size_t pos = s.find('.');
   if ( pos != std::string::npos)
   {
       pos = s.size() - (pos+1);
       s.erase(std::remove(s.begin(), s.end(), '.'), s.end());
       while(pos) { multiplier /= 10; pos--; }
   }
   else
        multiplier = 1;
   std::stringstream ss(s);
   int value;
   ss >> value;
   return value * multiplier;
}

Test code:

int main() {
      std::cout << Convert("35.28", 1000) << std::endl; //35.28 -> 35280
      std::cout << Convert("3.28", 1000)  << std::endl; //3.28  -> 3280
      std::cout << Convert("352.8", 1000) << std::endl; //352.8 -> 352800
      std::cout << Convert("35.20", 1000) << std::endl; //35.20 -> 35200
      std::cout << Convert("3528", 1000) << std::endl;  //no change
      return 0;
}

Output:

35280
3280
352800
35200
3528

Online Demo : http://ideone.com/uCujP

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    There isn't any consistency in your output. 3528 stays the same while 35.28 become 35280. 1000 means nothing, It doesn't mean sig digits, it doesn't guarantee alignment of decimals. What exactly are you trying to guarantee. Make no decimal strings (already int) multiply by original multiplier, not 1. – Lee Louviere Jul 21 '11 at 21:42
2

Remove dot char from string and convert it directly to int

ks1322
  • 33,961
  • 14
  • 109
  • 164
1

Do you mean stored as a string (char*)? Then you can create your own parser:

int flstrtoint(const char *str) {
    int r = 0;
    int i = strlen(str) - 1;

    while (i >= 0) {
        if (isdigit(str[i])) {
            r *= 10 
            r += str[i] - `0`;
        }
        i--;
    }

    return r;
}

flstrtoint("35.28"); // should return 3528
orlp
  • 112,504
  • 36
  • 218
  • 315
  • > // should return 3528 But OP wants 35280 for "35.28" and "35.280234" – unkulunkulu Jul 21 '11 at 19:03
  • That's simply adding `*= 10;` at the end, not really significant. Or first counting the position of the dot and then multiplying with the correct value. – orlp Jul 21 '11 at 19:04
0

Remove the dot from the char and then
Simplest but not the best use atoi

See my answer here, for other possible ways.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

As Als sais, use atoi, but with a twist, strip the string of the period and convert the result into int using atoi.

Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83