convert string with 2 decimals : "16.33"

Hello,

i have this string with two decimal places: "16.33", but whether i use atof,
stof or stold, i get 16.3 as a return.

some ideas on how to get the 16.33 ?

thanks
closed account (E0p9LyTq)
If your string is a C++ std::string, you can use the "convert from string" functions: std::stof, std::stod or std::stold. The conversions require a C++11 (or above) compiler.

http://www.cplusplus.com/reference/string/stof/
http://www.cplusplus.com/reference/string/stod/
http://www.cplusplus.com/reference/string/stold/
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main ()
{
   std::string orbits ("365.24128 29.53885");
   std::string::size_type sz;     // alias of size_t

   double earth = std::stod (orbits,&sz);
   double moon = std::stod (orbits.substr(sz));
   std::cout << std::fixed << earth << "\n" << moon << "\n";
}


365.241280
29.538850
yeah this is what i was doing. but actually it works, value of my string was "16.3" and not "16.33" due to early stop in reading a file...

thanks anyway !
Topic archived. No new replies allowed.