2012-02-14 56 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); 
}