2012-02-14 55 views
0

我已经看过扔在Converting Double to String in C++Convert double to string C++? 的建议,但我被要求创建一个函数的非OOP采取了一倍,并返回一个std :: string的只用数学。我已经能够做到这一点在过去与int int:双字符串C++使用数学

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; 
} 

但问题是,它使用检查大于零继续前进。我一直在想这样

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

我的事情,这应该在10%才去,但我不知道。每次我尝试我得到一个整数,而不是小数。

例如我给它125.5(251/2的结果),它输出1255.虽然在某些情况下我只得到125.任何帮助都会很好。

更新:选择的解决方案

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; 
} 
+0

['std :: to_string' from ](http://en.cppreference.com/w/cpp/string/basic_string/to_string)。 – Xeo 2012-02-14 06:25:23

+0

@Xeo我被要求使用数学算法而不是方法调用 – gardian06 2012-02-14 06:26:22

回答

2

你必须使用数学,你不能使用ostringstream

否则,你可以做两个部分:

  1. 首先得到的整数部分。
  2. 然后得到小数部分。

要获得整数部分,只需将该值转换为int即可。要自行获取小数部分,从值中减去整数部分。

当你有分数时,乘以十,并得到该值作为一个整数,这将是一个一位数的整数。

编辑:添加的代码示例

#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; 
} 

注:上述功能暴露出一些浮点数有计算机上的问题。例如,值1.234成为字符串"1.2339999999999999857891452847979962825775146484375"

+0

值可以是任何一个双精度可以是[+ -1.7E(+ - 308)]的数字不会只是一个十进制值,例如 – gardian06 2012-02-14 06:52:07

+0

I确定有更好的方法,但是你可以乘以10直到达到小数分量的极限,然后截断字符串末尾的零。 – Pochi 2012-02-14 07:10:04

+0

@ gardian06尝试我刚添加的功能。 – 2012-02-14 07:17:10

1

你可以在C使用ftoa,它看起来像这样

void ftoa(char * buf, double f, char pad) 
{ 
    char * p; 

    f = (f*100.0 + 0.5)/100.0;  // round 
    p = itoa(buf,f,pad); 
    *p++ = '.'; 

    f -= (unsigned int) f; 
    f = f * 100.0; 
    itoa(p,f,2); 
} 

或CPP

#include <iomanip> 

std::string doubleToString(double val) 
{ 
    std::ostringstream out; 
    out << std::fixed << val; 
    return out.str() 
} 

您还可以考虑使用setprecision设置的小数位数:

out << fixed << setprecision(2) << val; 
0

以这种方式保留你的toString(int)和toString(double)的重载

#include <cmath> 

std::string toString(int i) {...} 
std::string toString(double d) 
{ 
    const K = 1000000; // controls how many digits display in the fractional part 
    return toString(int(d)) + "." + toString(int(fabs(d) * K) % K); 
}