String and Floating Point Conversion Help

This is a discussion on String and Floating Point Conversion Help within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having some trouble with converting strings to currency/floats. First off I calculate monetary values at runtime and ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Question 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. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    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.

    What is that <algorithm> header?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    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. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by tabstop View Post
    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;
    }
    Last edited by anon; 05-03-2008 at 06:36 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Quote Originally Posted by Daved View Post
    >> 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. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if it's not idustry standard
    stl is industry standard...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Daved View Post
    >> 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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Daved View Post

    >> 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()
    );
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Thumbs up

    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String conversion to floating point[Digit grouping]
    By force of will in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2009, 01:53 PM
  2. convert a floating point number to string
    By heeroyung in forum C Programming
    Replies: 1
    Last Post: 10-02-2005, 01:03 AM
  3. exact conversion of floating point to string
    By szeliat in forum C Programming
    Replies: 6
    Last Post: 07-27-2005, 09:43 PM
  4. floating point to string
    By Sathyabodh in forum C Programming
    Replies: 2
    Last Post: 08-13-2003, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21