String functions --- detailed description and implementation of the atof () function (full version), --- detailed description of the atof Function

Source: Internet
Author: User

String functions --- detailed description and implementation of the atof () function (full version), --- detailed description of the atof Function

Atof () function

Atof (): double atof (const char * str );

Function: converts a string to a floating point number.

Str: string to be converted.

Return Value: double value returned for each function. This value is generated by parsing the input character as a number. If the input cannot be converted to a value of this type, the return value is 0.0.

Function Description: atof () scans the nptr parameter string and skips the leading space character until conversion starts when a number or positive or negative sign is encountered, the conversion ends only when a non-number or string ends ('\ 0') and returns the result. The str string can contain positive and negative numbers, decimal points, or E (e) to represent the exponential part.

# Include <iostream> using namespace std; double atof_my (const char * str) {double s = 0.0; double d = 10.0; int jishu = 0; bool falg = false; while (* str = '') {str ++;} if (* str = '-') // records positive and negative numbers {falg = true; str ++ ;} if (! (* Str> = '0' & * str <= '9') // If the start is not a number, the system returns 0.0 return s; while (* str> = '0' & * str <= '9' & * str! = '. ') // Calculate the integer before the decimal point {s = s * 10.0 + * str-'0'; str ++;} if (* str = '. ') // str ++; while (* str> = '0' & * str <= '9 ') // calculate the fractional part {s = s + (* str-'0')/d; d * = 10.0; str ++ ;} if (* str = 'E' | * str = 'E') // consider scientific notation {str ++; if (* str = '+ ') {str ++; while (* str> = '0' & * str <= '9') {jishu = jishu * 10 + * str-'0 '; str ++;} while (jishu> 0) {s * = 10; jishu -- ;}} if (* str = '-') {str ++; while (* str> = '0' & * str <= '9') {jishu = jishu * 10 + * str-'0'; str ++ ;} while (jishu> 0) {s/= 10; jishu --;}}} Return s * (falg? -1.0: 1.0);} int main () {char * s1 = "123.456567567e-10"; char * a1 = "123.456567567e-10 "; char * s2 = "1234567.235e + 10"; char * a2 = "1234567.235e + 10"; char * s3 = "123.45656 \ 07567e-10"; char * a3 = "123.45656 \ 07567e-10 "; double sum_1 = atof_my (s1); double sum1 = atof (a1); double sum_2 = atof_my (s2); double sum2 = atof (a2); double sum_3 = atof_my (s3 ); // The End double sum3 = atof (a3); cout <"atof_my:" <sum_1 <endl; cout <"atof: "<sum1 <endl; cout <" atof_my: "<sum_2 <endl; cout <" atof: "<sum2 <endl; cout <"atof_my:" <sum_3 <endl; cout <"atof:" <sum3 <endl; system ("pause"); return 0 ;}
Running result comparison diagram:




In this example, the atof function is used to convert the numeric type into a numerical value change program.

Double atof (const char * s)
{
Double val, power;
Int sign, I;
// Remove spaces from the string
For (I = 0; isspace (s [I]) & s [I]! = 0; ++ I)
;
// Judge Positive and Negative Numbers and point I to the next position
Sign = (s [I] = '-')? -1:1;
If (s [I] = '+' | s [I] = '-')
I ++;
// Retrieve the integer part of the Floating Point Number
For (val = 0.0; isdigit (s [I]) & s [I]! = 0; ++ I)
Val = 10 * val + s [I]-'0 ';
// Decimal point
If (s [I] = '.')
I ++; // the last position
// Obtain the number after the decimal point
For (power = 1.0; isdigit (s [I]) & s [I]! = 0; ++ I)
{
Val = 10 * val + s [I]-'0 ';
Power * = 10; // number of digits in decimal places
}
Return sign * val/power; // calculate the floating point number.
}
Hi.baidu.com/..a.html

What is the usage of the atof function?

Welcome
Function Name: atof
Function: converts a string to a floating point number.
Source: array to floating point numbers
Usage: double atof (const char * nptr );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Float f;
Char * str = "12345.67 ";
F = atof (str );
Printf ("string = % s float = % f \ n", str, f );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.