Double My_atof (char *str)

Source: Internet
Author: User

Write a function that converts a numeric string to a number corresponding to the string (including positive floating-point numbers, negative floating-point numbers)

For example: "12.34 " returns 12.34 "123.34" return-123.34 function prototype: Double my_atof (char *str)

#include <stdio.h> #include <ctype.h> #include <cmath>double my_atof (char *str) {DOUBLE ret = 0.0;int Sign = 1;char *point;while (*str! = ') {if (Isspace (*STR))//isspace Determine the space str++;else if (*str = = '-') {sign = -1;str++;} else if (*str = = ' + ') str++;else if (*str = = '. ') {point = str;str++;} else if ((*str >= ' 0 ') && (*str <= ' 9 ')) {Ret=ret * 10.0 + (*STR-' 0 ');//character and number difference between characters 0str++;}} ret = Sign*ret/pow (10,str-point-1); return ret;} int main () {double Ret;char arr[100];scanf ("%s", arr), ret = My_atof (arr);p rintf ("%f\n", ret); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Double My_atof (char *str)

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.