# 需要帮助将字符串转换为浮点数... [英] Need help converting a string to a float...

### 问题描述

27000000.0 -27000000.00

2973999.99 -29740000.00

2989999.13 -2989999.25

string tokens [14];

stringstream iss;

getline（fin，bfr）;

while（getline（fin，bfr） ）

{

iss<< bfr;

i = 0;

while（getline（iss，token，''，''））

{

}

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

string tokens [14];

stringstream iss;

getline（fin，bfr）;

while（getline（fin，bfr） ）

{

iss<< bfr;

i = 0;

while（getline（iss，token，''，''））

{

}

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）;

Could be.

Here''s code I used to display the numbers side by side:

printf("%s %10.2f\n",tokens[0].c_str(), price);

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

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

string tokens [14];

stringstream iss;

getline（fin，bfr）;

while（getline（fin，bfr） ）

{

iss<< bfr;

i = 0;

while（getline（iss，token，''，''））

{

}

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());