-2

i have string

std::string MegaNum( float 10020030040 )

The important thing is to convert my string so that we can put a space for every 3 digit. How we are gonna group them is not as important.

i want to convert to 4 new values with 1-3 digits value

float 1; /*with value 10*/
float 2; /*with value 020*/
float 3; /*with value 030*/
float 4; /*with value 040*/

to then print it out as

10 020 030 040

Sid S
  • 6,037
  • 2
  • 18
  • 24
  • Value of `020` should really be `20`, right? `020` is not a number, it's a sequence of digits. Is it mandatory to convert the `std::string` to `float`s? Couldn't you just insert spaces every 3 characters from the end of the original string? – Fureeish May 05 '18 at 00:15
  • This isn't a free homework service. Please show us what you've tried so far to solve the problem, and why you're stuck. http://idownvotedbecau.se/noattempt/ Here's a starting point: http://en.cppreference.com/w/cpp/string/basic_string/insert. A solution using this isn't very efficient, but it should be good enough for what you're doing. – eesiraed May 05 '18 at 01:01
  • Possible duplicate of [How to insert spaces in a big number to make it more readable?](https://stackoverflow.com/questions/7257956/how-to-insert-spaces-in-a-big-number-to-make-it-more-readable) – Sam Rockett May 05 '18 at 01:27
  • @Fureeish no thats what im trying to say it's not mandatory. The main point is to convert a number forexample like 1010 dollars in to 1 010 dollars or a phone number – Morris Simons May 05 '18 at 16:16
  • @FeiXiang i tryd a couple of metods in fact 3 diffrent ones. The closest i come is converting a 1020 to 1 0 2 0 but i cant seem to figure out how to group them. i seen diffrent solutions for diffrent programming languages but not for C++. And btw im createing a program that calculate intrest on a loan put i want to format it nicely. – Morris Simons May 05 '18 at 16:21
  • If you tried something, you should mention it in the question. The community dislikes it a lot when someone asks "How to do X?" without showing what they have tried. Why not just get the number as a string in decimal by inputting it as a string or converting it with [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string), iterate through it from right to left, and inserting a space or comma every three digits? – eesiraed May 06 '18 at 00:23

1 Answers1

0

convert my string so that we can put a space for every 3 digit.

Since you are starting with a string, you might consider just doing the the deed directly (no float math).

The following function takes a string and counts off 3 digits from the least significant char (the right most) and returns the digits with spaces inserted (the line grows). (it does not confirm validity of number, does not check that chars are digits)

std::string digiSpace3(std::string s)
   {  //      sSize must be signed int of sufficient size
      int32_t sSize = static_cast<int32_t>(s.size());
      if (sSize > 3)
         for (int32_t indx = (sSize - 3); indx > 0; indx -= 3)
            s.insert(static_cast<size_t>(indx), 1, ' ');
      return(s);
   }

used like:

std::string MegaNum = "10020030040" ;
// std::string MegaNum  ( std::to_string(10020030040) ) ; //also works

std::cout << "\n  " << digiSpace3(MegaNum) << std::endl;

and produces

  10 020 030 040

fyi - my compiler does not accept

std::string MegaNum( float 10020030040 );

and reports

 error: expected ‘,’ or ‘...’ before numeric constant
2785528
  • 5,438
  • 2
  • 18
  • 20