istringstream float

Hello everyone, I have to take it from a text file to a string, the string in question is a float number.
If you do it this way:
1
2
3
getline (fin, line);
istringstream (line) >> p;
cout << p;

I will return the number up, can you tell me how I can do.
Eg
310456.8 me back 310 457
Without the space:
1
2
3
4
5
6
7
getline(fin, line);
istringstream sin(line);

double d;
sin >> d;

cout << long(d + 0.5);
does not work, then I need to keep the number to a float variable does not print it.
I'm sorry angelk, I'm having trouble with your English.
I will return the number up

What does "number up"mean?

does not work

Please be specific about what is not working. Not compiling? Not executing? Wrong output? Or?

I need to keep the number to a float variable

Then simply change kbw's example to a float.
1
2
3
4
5
6
7
getline(fin, line);
istringstream sin(line);

float f;
sin >> f;

cout << f << endl; 

If that's not what you want, then please explain what you want more clearly.



The number you get is an approximate number, I would like to get the correct number is not approximated.
1
2
3
4
5
double approximate_number = 310456.8 ;

// rounded away from zero in halfway cases -2.5 rounded to -3, +2.5 rounded to +3
// http://en.cppreference.com/w/cpp/numeric/math/round
long long number_rounded_to_nearest_integer = std::llround( approximate_number ) ; // 310457 
I have this number 420898.7, in a line of a text file. I want to put on a float variable this number. xD
1
2
3
4
5
6
7
8
9
10
11
// step 1. read the line
std::string line ;
std::getline(fin, line);

// step 2. use a string stream to extract the number into a float
std::istringstream sstm(line);
float number ;
sstm >> number ;

// step 3. round to nearest whole number
number = std::round( number ) ; // http://en.cppreference.com/w/cpp/numeric/math/round 
But I do not want the number is rounded.
can you tell me how I can do.
Eg
310456.8 me back 310 457


Then later:
But I do not want the number is rounded.

But that is what you're asking for,.
Topic archived. No new replies allowed.