需要帮助将字符串转换为浮点数... [英] Need help converting a string to a float...
问题描述
我有这个奇怪的问题我的代码执行以下操作
从字符串转换为浮点数:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
左边的数字是我在标记更大的字母后获得的字符串
字符串。右边的数字是转换后得到的数字。
我尝试了两种不同的转换方式,一种是老C方式
使用atof,以及另一种更多的C ++方式。无论哪种方式,我得到相同的
结果。谁能为我解释一下这个问题?
这里的代码是:
字符串代币;
string tokens [14];
stringstream iss;
getline(fin,bfr);
while(getline(fin,bfr) )
{
iss<< bfr;
i = 0;
while(getline(iss,token,'',''))
{
令牌[i ++] =令牌;
}
std :: istringstream b(令牌[0]);
b>价格;
// price = atof(tokens [0] .c_str());
I''m having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left is the string I get after tokenizing a bigger
string. The number on the right is the number I get after the conversion.
I''ve tried two different ways of converting, one way the older C way
using atof, and another more C++ way. Either way, I get the same
result. Can anyone shed some light on this for me?
Here''s the code:
string token;
string tokens[14];
stringstream iss;
getline(fin,bfr);
while (getline(fin, bfr))
{
iss << bfr;
i = 0;
while (getline(iss, token, '',''))
{
tokens[i++] = token;
}
std::istringstream b(tokens[0]);
b >price;
//price = atof(tokens[0].c_str());
推荐答案
6月7日下午2:52,Hank Stalica< u ... @ example.netwrote:
On Jun 7, 2:52 pm, Hank Stalica <u...@example.netwrote:
我有这个奇怪的问题我的代码执行以下操作
从字符串转换为浮点数:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
左边的数字是我在标记更大的
字符串后得到的字符串。右边的数字是转换后得到的数字。
我尝试了两种不同的转换方式,一种是老C方式
使用atof,以及另一种更多的C ++方式。无论哪种方式,我得到相同的
结果。谁能为我解释一下这个问题?
这里的代码是:
字符串代币;
string tokens [14];
stringstream iss;
getline(fin,bfr);
while(getline(fin,bfr) )
{
iss<< bfr;
i = 0;
while(getline(iss,token,'',''))
{
令牌[i ++] =令牌;
}
std :: istringstream b(令牌[0]);
b>价格;
// price = atof(tokens [0] .c_str());
I''m having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left is the string I get after tokenizing a bigger
string. The number on the right is the number I get after the conversion.
I''ve tried two different ways of converting, one way the older C way
using atof, and another more C++ way. Either way, I get the same
result. Can anyone shed some light on this for me?
Here''s the code:
string token;
string tokens[14];
stringstream iss;
getline(fin,bfr);
while (getline(fin, bfr))
{
iss << bfr;
i = 0;
while (getline(iss, token, '',''))
{
tokens[i++] = token;
}
std::istringstream b(tokens[0]);
b >price;
//price = atof(tokens[0].c_str());
我不认为转换是你的问题,我认为你的方法是输出你的数字导致四舍五入和混乱...
JMO。
I don''t think conversion is your problem, I think its your method of
outputting your numbers that is causing rounding and confusion...
JMO.
可能。
这是我用过的代码并排显示数字:
printf("%s%10.2f \ n",tokens [0] .c_str(),price);
SpreadTooThin写道:
Could be.
Here''s code I used to display the numbers side by side:
printf("%s %10.2f\n",tokens[0].c_str(), price);
SpreadTooThin wrote:
6月7日下午2:52,Hank Stalica< u ... @ example.netwrote:
On Jun 7, 2:52 pm, Hank Stalica <u...@example.netwrote:
>我有这个奇怪的问题,我的代码执行以下
从字符串到float的转换:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
左边的数字是我在标记更大的字符串后得到的字符串。右边的数字是我转换后得到的数字。
我尝试了两种不同的转换方式,一种是旧的C方式使用atof,另一种是C ++办法。无论哪种方式,我得到相同的结果。任何人都可以为我阐明这一点吗?
这里是代码:
字符串标记;
字符串标记[14];
stringstream iss;
getline(fin,bfr);
while(getline(fin,bfr))
{
iss<< bfr;
i = 0;
while(getline(iss,token,'',''))
{
tokens [i ++] = token;
}
std :: istringstream b(tokens [0]);
b>价格;
// price = atof(tokens [0] .c_str( ));
>I''m having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left is the string I get after tokenizing a bigger
string. The number on the right is the number I get after the conversion.
I''ve tried two different ways of converting, one way the older C way
using atof, and another more C++ way. Either way, I get the same
result. Can anyone shed some light on this for me?
Here''s the code:
string token;
string tokens[14];
stringstream iss;
getline(fin,bfr);
while (getline(fin, bfr))
{
iss << bfr;
i = 0;
while (getline(iss, token, '',''))
{
tokens[i++] = token;
}
std::istringstream b(tokens[0]);
b >price;
//price = atof(tokens[0].c_str());
我不认为转换是你的问题,我认为你的方法输出你的数字导致四舍五入和混乱...
JMO。
I don''t think conversion is your problem, I think its your method of
outputting your numbers that is causing rounding and confusion...
JMO.
我使用以下代码来验证它是显示问题还是实际
转换问题:
if(price == 2989999.13)
cout<< "匹配" << endl;
并没有显示匹配,所以看起来转换正在以某种方式搞砸了
。
Hank Stalica写道:
I used the following code to verify if it was display issue or an actual
conversion issue:
if (price == 2989999.13)
cout << "Match" << endl;
And match was not displayed, so it looks like the conversion is getting
screwed up somehow.
Hank Stalica wrote:
我有这个奇怪的问题我的代码执行以下操作
从字符串转换为浮点数:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
左边的数字是我在标记更大的
字符串后得到的字符串。右边的数字是转换后得到的数字。
我尝试了两种不同的转换方式,一种是老C方式
使用atof,以及另一种更多的C ++方式。无论哪种方式,我得到相同的
结果。谁能为我解释一下这个问题?
这里的代码是:
字符串代币;
string tokens [14];
stringstream iss;
getline(fin,bfr);
while(getline(fin,bfr) )
{
iss<< bfr;
i = 0;
while(getline(iss,token,'',''))
{
令牌[i ++] =令牌;
}
std :: istringstream b(令牌[0]);
b>价格;
// price = atof(tokens [0] .c_str());
I''m having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left is the string I get after tokenizing a bigger
string. The number on the right is the number I get after the conversion.
I''ve tried two different ways of converting, one way the older C way
using atof, and another more C++ way. Either way, I get the same
result. Can anyone shed some light on this for me?
Here''s the code:
string token;
string tokens[14];
stringstream iss;
getline(fin,bfr);
while (getline(fin, bfr))
{
iss << bfr;
i = 0;
while (getline(iss, token, '',''))
{
tokens[i++] = token;
}
std::istringstream b(tokens[0]);
b >price;
//price = atof(tokens[0].c_str());
这篇关于需要帮助将字符串转换为浮点数...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!