How To Change Int To Double In C++
C Linguistic communication: strtod function
(Convert String to Double)
In the C Programming Linguistic communication, the strtod function converts a string to a double.
The strtod function skips all white-space characters at the beginning of the string, converts the subsequent characters every bit part of the number, and so stops when it encounters the first character that isn't a number.
Syntax
The syntax for the strtod part in the C Language is:
double strtod(const char *nptr, char **endptr);
Parameters or Arguments
- nptr
- A pointer to a cord to convert to a double.
- endptr
- Information technology is used past the strtod function to betoken where the conversion stopped. The strtod function will alter endptr (if endptr is not a null pointer) so that endptr points to the first character that was not converted.
Returns
The strtod function returns the double representation of a string. The strtod function skips all white-infinite characters at the beginning of the cord, converts the subsequent characters as part of the number, and so stops when it encounters the first character that isn't a number.
If the strtod office converts a value that is besides large or likewise pocket-size to convert, information technology volition store ERANGE in errono.
Required Header
In the C Language, the required header for the strtod part is:
#include <stdlib.h>
Applies To
In the C Language, the strtod part can exist used in the post-obit versions:
- ANSI/ISO 9899-1990
strtod Instance
Allow's look at an example to run into how you would use the strtod office in a C plan:
/* Example using strtod past TechOnTheNet.com */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ char value[10]; char *eptr; double outcome; /* Re-create a value into the variable */ /* Information technology's okay to accept whitespace earlier the number */ strcpy(value, " 958"); /* Convert the provided value to a double */ result = strtod(value, &eptr); /* If the result is 0, test for an error */ if (result == 0) { /* If the value provided was out of range, display a warning message */ if (errno == ERANGE) printf("The value provided was out of range\n"); } /* Display the converted result */ printf("%f decimal\n", upshot); /* Copy a hexadecimal value into the variable */ strcpy(value, "0x8b2"); /* Catechumen the hexadecimal provided value to a double */ result = strtod(value, &eptr); /* If the result is 0, test for an mistake */ if (result == 0) { /* If the value provided was out of range, display a warning message */ if (errno == ERANGE) printf("The value provided was out of range\n"); } /* Brandish the converted event */ printf("%f decimal\n", result); return 0; }
When compiled and run, this application will output:
958.000000 decimal 2226.000000 decimal
Similar Functions
Other C functions that are similar to the strtod function:
- atof office <stdlib.h>
- strtol function <stdlib.h>
- strtoul function <stdlib.h>
Come across Also
Other C functions that are noteworthy when dealing with the strtod function:
- atoi office <stdlib.h>
- atol part <stdlib.h>
Source: https://www.techonthenet.com/c_language/standard_library_functions/stdlib_h/strtod.php
Posted by: steeleconswited1948.blogspot.com
0 Response to "How To Change Int To Double In C++"
Post a Comment