我想要得到int部分和double的小數部分。 這就是我實現了這一點:to_string函數附加零
void func(double d)
{
int intpart, decpart;
intpart = d;
string szDec = to_string(d);
szDec = szDec.substr(szDec.find(".") + 1);
decpart = stoi(szDec);
m_iNom = intpart * decpart + 1;
m_iDenom = decpart;
}
我傳遞給FUNC 0.5,試圖讓小數部分爲整數時出現的問題。
非但沒有5,to_String使得szDec = 「0.500000000000000」
並使用SUBSTR()函數時,szDec = 「0.500000」
離開我=的decpart 500000代替5.
如何停止to_String函數和substr函數將零附加到字符串並將其轉換爲浮點型?
剛剛使用[std :: modf](http://en.cppreference.com/w/cpp/numeric/math/modf)有什麼問題 – Cubbi 2014-09-27 15:24:38
@Cubbi說我有3.5,我需要3作爲5而不是3和0.5 – 2014-09-27 15:29:20
如果你有'3.1',那麼怎麼樣?當你把它存儲在一個雙精度,你會得到'3.100000000000000088817841970012523233890533447265625'。你想要'3'和'100000000000000088817841970012523233890533447265625'還是想要'3'和'1'?如果你想要'3'和'1',你想如何計算它們? – Cubbi 2014-09-27 15:38:39