本文介绍了需要帮助将字符串转换为浮点数...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个奇怪的问题我的代码执行以下操作

从字符串转换为浮点数:


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.




我不认为转换是你的问题,我认为你的方法输出你的数字导致四舍五入和混乱...

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.



这篇关于需要帮助将字符串转换为浮点数...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:55