Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:31:14+00:00 2026-05-29T19:31:14+00:00

I have already read throw the suggestions on Converting Double to String in C++

  • 0

I have already read throw the suggestions on Converting Double to String in C++ and
Convert double to string C++?
but I have been asked to create a function non-OOP to take in a double, and return a std::string using only math. I have been able to do this with an int in the past wich:

std::string toString(int value) {
    string result = "";
    bool negative = false;
    if ( value < 0 ) {
        negative = true;
        value *= (-1);
    }
    do {
        string tmpstr = "0";
        tmpstr[0] += value%10;
        result = tmpstr + result;
        value /= 10;
    } while ( value != 0);
    if ( negative ) {
        result = "-" + result;
    }
    return result;
}

but the problem is that it uses a check for greater then zero to keep going. I keep thinking something like

if ( value < 0 ){
    value *= 10;
}

I thing that this should go before the %10 but I’m not sure. every time I try I get a whole number, and not the decimal.

for example I give it 125.5 (result of 251/2), and it outputs 1255. though in some cases I only get 125. any help would be great.

Update:chosen solution

std::string toString(float value){
    bool negative = false;
    if ( value < 0 ) {
        negative = true;
        value *= (-1);
    }

    int v1 = value;         // get the whole part
    value -= v1;            // get the decimal/fractional part
    //convert and keep the whole number part
    std::string str1 = toString(v1); 

    std::string result = "";
    do {
        value *= 10;
        int tmpint = value;
        value -= tmpint;
        string tmpstr = "0";
        tmpstr[0] += tmpint % 10;
        result = tmpstr + result;
        value /= 10;
    } while ( value != 0);

    result = str1 + "." + result;

    if ( negative ) {
        result = "-" + result;
    }
    return result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:31:15+00:00Added an answer on May 29, 2026 at 7:31 pm

    Do you have to use math, can’t you use an ostringstream?

    Otherwise you can do it in two parts:

    1. First get the integer part.
    2. Then get the fraction part.

    To get the integer part, just cast the value to an int. To get the fraction part by itself, subtract the integer part from value.

    When you have the fractions, multiply by ten and get the that value as an integer, which will be a single-digit integer.

    Edit: Added code sample

    #include <string>
    #include <algorithm>   // for std::reverse
    #include <cmath>       // for std::fabs
    
    // ...
    
    std::string toString(double value)
    {
        // Get the non-fraction part
        int ival = int(value);
    
        // Get the fraction part
        double frac = std::fabs(value - ival);
    
        // The output string
        std::string str;
    
        // Is the number negative?
        bool negative = false;
    
        // Convert the non-fraction part to a string
        if (ival < 0)
        {
            negative = true;
            ival = -ival;
        }
    
        while (ival > 0)
        {
            str += char((ival % 10) + '0');
            ival /= 10;
        }
    
        // The integer part is added in reverse, so reverse the string to get it right
        std::reverse(str.begin(), str.end());
    
        if (negative)
            str = std::string("-") + str;
    
        if (frac > 0.0)
        {
            str += '.';
    
            // Convert the fraction part
            do
            {
                frac *= 10;
                ival  = int(frac);
                str  += char(ival + '0');
                frac -= ival;
            } while (frac > 0.0);
        }
    
        return str;
    }
    

    Note: The above function exposes some of the problems floating point numbers have on computers. For example, the value 1.234 becomes the string "1.2339999999999999857891452847979962825775146484375".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What’s with the love of dynamic Languages I have already read this
I have an XmlDocument that already exists and is read from a file. I
I have already posted something similar here but I would like to ask the
I have already created a webpart to show the data from list, but I
I have already posted a question about this, but the situation has changed sufficiently
I have already search for some time over the net on how to create
I have already read the hibernate docs about concurrency and I think, I have
I have already read Using Java to encrypt integers and Encrypting with DES Using
I have already tried PreRenderComplete and unload is too late
I have already googled for this I have a Table with following structure in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.