C++中_atold库函数问题

来源:百度知道 编辑:UC知道 时间:2024/05/18 15:32:49
做以一个程序要把输入的形如$123,456,789.99(金融类型)的字符串转换成long double型
#include <iostream>
#include <STDLIB.h>
#include <math.h>
#include <string>
using namespace std;

long double mstold(string);

int main()
{
string s1;
cout<<"\nEnter the string: "<<endl;
getline(cin,s1);

mstold(s1);
return 0;
}

long double mstold(string s1)
{
long double c;
char ch[100];
int a,b;
s1.erase(0,1);
for(;;)
{ a=s1.find(",");
if(a != -1)
s1.erase(a,1);
else
break;
}
b=s1.length();
s1.copy(ch, b, 0);
ch[b]=0;
return c=_atold(ch);

}
此程序调试时提示error C2065: '_atold' : undeclared identifier,ch字符串可以正常输出,为什么我的_atold库函数用不成呢?

不知道你是不是想要这样的程序,能将“123,456,789.99”字符串转换成123456789.99的double类型,至于long doubleC++中没有从string转换long double的函数,atof可以从cstr转换成double:
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>

using namespace std;

int main ()
{
double transformer(string&);
string money_s("123,456,78.99");
double modey_d(transformer(money_s));
cout << setprecision(10) << modey_d << endl;
}

double transformer(string& s)
{
while(s.find(",") != string::npos) {
s.erase(s.find(","), 1);
}
return atof(s.c_str());
}

出出结果为:12345678.99

标准库里没有 _atold 函数。

如果你已把逗号和$ 滤掉,你用
long double c;
char ch[100];
sscanf(ch,"%lf",&c);
就转换成 double 了。