嘿所以我正在返回有小數,整數,或總數的數的函數,但一直未能使其與以下任一方式工作:如何計算一個大雙數中有多少個小數位?
乘以一個真正大量像10十億不會因爲innacurate路電腦店小數的工作,使2.3 2.2999575697
使用字符串流將數字轉換爲字符串,並計算字符不起作用,因爲它要求您將流設置爲精確度如果未設置爲實際的小數位數,則要麼帶走要麼添加不需要的'0'字符
那麼我現在該做什麼?有人請幫助=(感謝
,如果你想看到我的函數的麻木轉換爲字符串在這裏它是:
////////////////////// Numbs_Digits ////////////////////////////////////////////////
template<typename T>
int Numbs_Digits(T numb, int scope)
{
stringstream ss(stringstream::in| stringstream::out), ss2(stringstream::in| stringstream::out);
unsigned long int length= 0;
unsigned long int numb_wholes;
ss2 << (int)numb;
numb_wholes = ss2.str().length(); ss2.flush();
bool all= false;
ss.precision(11); // HOW DO I MAKE THE PRECISION NUMBER THE NUMBER OF DECIMALS?
switch(scope){
case ALL: all = true;
case DECIMALS: ss << fixed << numb;
length += ss.str().length()- (numb_wholes +1); // +1 for the "."
if(all!= true) break;
case WHOLE_NUMBS:
length += numb_wholes;
if(all!= true) break;
default: break;}
return length;};
而不是給我們你的示例代碼,它可能會更好,如果你能給出一個更清晰的問題描述。請提供一些數字和你期望的結果的例子。 – 2011-05-16 01:37:19
從你以前的問題來看,我建議閱讀浮點實際工作原理。那麼你就會明白爲什麼「雙數中的小數」沒有多大意義。 – hammar 2011-05-16 01:44:14
可能使用http:// stackoverflow。com/questions/4738768/printing-double-without-losing-precision – ThomasMcLeod 2011-05-16 03:42:07