C + + string converted to floating point type

Source: Internet
Author: User

The first type:

[CPP]View PlainCopy
    1. Char szstring[] = "3.1415926535898";
    2. Double db1;
    3. DB1 = Atof (szstring);
    4. printf ("Atof result:\n");
    5. printf ("%f%.12f%.2f%e%e\n", DB1, DB1, DB1, DB1, DB1);
    6. printf ("%.1e%.1e%.18e%.18e\n", DB1, DB1, DB1, DB1);


The second type:

[CPP]View PlainCopy
    1. Char szstring2[] = "3.1415926535898";
    2. Double DB2;
    3. SSCANF (SzString2, "%lf", &DB2);
    4. printf ("\nsscanf result:\n");
    5. printf ("%f%.12f%.2f%e%e\n", DB2, DB2, DB2, DB2, DB2);
    6. printf ("%.1e%.1e%.18e%.18e\n", DB2, DB2, DB2, DB2);


There are many magical functions for the SSCANF function. Let's look at the use of the encyclopedia:

1. Common usage.

Char buf[512];

SSCANF ("123456", "%s", buf);//Here buf is the name of the array, which means to deposit 123456 in buf in the form of%s!

printf ("%s\n", buf);

The result is: 123456

2. Take a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.

SSCANF ("123456", "%4s", buf);

printf ("%s\n", buf);

The result is: 1234

3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.

SSCANF ("123456 Abcdedf", "%[^]", buf);

printf ("%s\n", buf);

The result is: 123456

4. Take a string that contains only the specified character set. As in the following example, take a string that contains only 1 to 9 and lowercase letters.

SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);

printf ("%s\n", buf);

The result is: 123456ABCDEDF

When entering:

SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);

printf ("%s\n", buf);

The result is: 123456

5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.

SSCANF ("123456abcdedfBCDEF", "%[^a-z]", buf);

printf ("%s\n", buf);

The result is: 123456ABCDEDF

6, given a string iios/[email protected], gets/And the string between @, first filter out "iios/", and then send a string of non-' @ ' to BUF

SSCANF ("Iios/[email protected", "%*[^/]/%[^@]", buf);

printf ("%s\n", buf);

The result is: 12DDWDFF

C + + string converted to floating point type

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.