# Thread: String and Floating Point Conversion Help

1. ## String and Floating Point Conversion Help

Hello, I am having some trouble with converting strings to currency/floats.

First off I calculate monetary values at runtime and display them on a stringgrid by using FloatToStrF(<value>, ffCurrency, 10, 2).

Later I need to grab some of these values from the stringgrid and use them to calculate other values to be stored on the grid. The problem is that I am having trouble with converting the strings back into a valid floating point number (since they have a leading dollar sign and have thousands separators. Is there a function that can convert a string into some floating point format, when the string contains a currency symbol and thousands separators?

StrToCurr and StrToFloat are both out. I could write my own function to do this, but I don't want to if there is already a function to do this (no point in reinventing the wheel).

Any help would be appreciated. Please, let me know if I haven't made myself clear enough.

Thanks.

2. I would try something like this for the start:
Code:
```#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>

int main()
{
std::string number("\$1,098.98");
std::remove(number.begin(),number.end(),',');
std::stringstream ss(number);
char dollar;
float value;
ss >> dollar >> value;

std::cout << value << std::endl;
return 0;
}```

3. When I tested out that code, I received "1" as my output.

Thanks for the suggestion, however; I am not using the string class. I prefer to manage strings myself as char arrays.

4. Both gcc and visual studio give 1098.99 with that code. So I'm not sure how you're only getting 1. algorithm is a standard C++ header which coincidentally contains std::remove.

If you're using char arrays, then loop through it with a spare char array to hand; if you see a number or a decimal point, put it in the spare char array. Then use strtof.

5. That was what I was going to do if there wasn't a function in existence.

Thanks for the help guys!!!

I appreciate it. Had I not come here, I would have always wondered if there was some other way that I was unable to find.

6. >> I prefer to manage strings myself as char arrays.

Just curious, but why? I see a lot of people who do this but not a lot of good reasons. Normally it's because that's what they were taught (unfortunately) and they are hesitant to try the better solution because it is new to them.

7. Originally Posted by tabstop
Both gcc and visual studio give 1098.99 with that code.
Yet the input was "\$1,098.98".
You forgot one step: actually erasing unused characters from the end of the string:
Code:
`number.erase(std::remove(number.begin(),number.end(),','));`
By the way, algorithms work on char arrays too:
Code:
```#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

int main()
{
char number[] = "\$1,098.98";
*std::remove(number, number + strlen(number), ',') = '\0';
std::stringstream ss(number);
char dollar;
float value;
ss >> dollar >> value;

std::cout << value << std::endl;
return 0;
}```

8. >> You forgot one step: actually erasing unused characters from the end of the string

That's not really necessary here since the leftovers just get left in the stringstream. Of course, I'd probably use erase anyway.

9. Originally Posted by Daved
>> I prefer to manage strings myself as char arrays.

Just curious, but why? I see a lot of people who do this but not a lot of good reasons. Normally it's because that's what they were taught (unfortunately) and they are hesitant to try the better solution because it is new to them.
I'm another one of those people. I was never taught the string class because the instructor felt that it had issues (which we weren't told about). I have never had any troubles using char arrays and just haven't taken the time to learn the string class.

Is it widely used and advisable to learn? I see no reason to learn it, if it's not idustry standard, but please tell me.

Thanks

10. if it's not idustry standard
stl is industry standard...

11. Originally Posted by Daved
>> You forgot one step: actually erasing unused characters from the end of the string

That's not really necessary here since the leftovers just get left in the stringstream. Of course, I'd probably use erase anyway.
Well, in this case std::remove (at least in some implementations) turns "\$1,098.98" into "\$1098.988" which is a different number than the original value.

12. I was never taught the string class because the instructor felt that it had issues (which we weren't told about).
It does have design issues, though I would think that those would not come under consideration when teaching beginners.

stl is industry standard...
std::string was never part of the STL
But yeah, it is part of the C++ Standard library.

13. >> Is it widely used and advisable to learn?
Yes and yes. The string class is standard and is available on any remotely modern compiler. It's basically part of the language.

Even if you are an expert in C style strings, the string class will save you time and make your code easier to write and read.

>> Well, in this case std::remove (at least in some implementations) turns "\$1,098.98" into
>> "\$1098.988" which is a different number than the original value.

You're right. I was assuming the ',' got moved to the end, but that wouldn't make sense.

14. Originally Posted by Daved

>> Well, in this case std::remove (at least in some implementations) turns "\$1,098.98" into
>> "\$1098.988" which is a different number than the original value.

You're right. I was assuming the ',' got moved to the end, but that wouldn't make sense.
Well, my string example is a bit wrong too. It should use the string version that takes two arguments:
Code:
```number.erase(
std::remove(number.begin(), number.end(), ','),
number.end()
);```

15. I guess I'll have to read up on the string class this summer. I'm sure I'll like it, I have always had grief with char arrays and their programmer-unfriendliness.

For now, I have made a simple function that copies everything except currency symbols and commas to a new array that is converted to a float... Cheap and cheesy, but it'll work for now.

Thanks guys!