Материал из C\C++ эксперт
Using atof.
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::atof;
int main()
{
double d = atof( "99.0" );
cout << d / 2.0 << endl;
return 0;
}
49.5
Using atoi
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
int main()
{
int i = atoi( "2593" );
cout << i - 593 << endl;
return 0;
}
2000
Using atol
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::atol;
int main()
{
long x = atol( "1000000" );
cout << x / 2 << endl;
return 0;
}
500000
Using strtod
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::strtod;
int main()
{
double d;
const char *string1 = "51.2 are admitted";
char *stringPtr;
d = strtod( string1, &stringPtr );
cout << d << endl;
return 0;
}
51.2
Using strtol
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::strtol;
int main()
{
long x;
const char *string1 = "-1234567abc";
char *remainderPtr;
x = strtol( string1, &remainderPtr, 0 );
cout << x << " " << remainderPtr << " " << x + 567 << endl;
return 0;
}
-1234567 abc -1234000
Using strtoul
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib> // strtoul prototype
using std::strtoul;
int main()
{
unsigned long x;
const char *string1 = "1234567abc";
char *remainderPtr;
x = strtoul( string1, &remainderPtr, 0 );
cout << x << " "<< x - 567 << endl;
return 0;
}
1234567 1234000