How to convert a string to a floating point

Source: Internet
Author: User

Method 1:

    1. char szstring [] = "-2876.99812376443 " ;
    2. double db1;
    3. db1 = atof (szstring);
    4. printf ( "atof result: \ n " );
    5. printf ( "% F %. 12f %. 2f % E \ n " , db1, db1, db1, db1, db1);
    6. printf ( "%. 1e %. 1e %. 18e %. 18e \ n " , db1, db1, db1, db1);

Method 2:

  1. CharSzstring2 [] ="-2876.99812376443";
  2. DoubleDB2;
  3. Sscanf (szstring2,"% Lf", & DB2 );
  4. Printf ("\ Nsscanf result: \ n");
  5. Printf ("% F %. 12f %. 2f % E \ n", DB2, DB2, DB2, DB2, DB2 );
  6. Printf ("%. 1E %. 1E %. 18e %. 18e \ n", DB2, DB2, DB2, DB2 );

The two methods can get the same output! The output is as follows:

Atof result:
-2876.998124-2876.998123764430-2877.00-2.876998e + 003-2.876998e + 003
-2.9e + 003-2.9e + 003-2.876998123764108100e + 003-2.876998123764108100e + 003

Sscanf result:
-2876.998124-2876.998123764430-2877.00-2.876998e + 003-2.876998e + 003
-2.9e + 003-2.9e + 003-2.876998123764108100e + 003-2.876998123764108100e + 003

 

Many people do not know much about the functions of the sscanf family. I want to write down the specific usage of the functions of the sscanf family. I hope you can make common progress. If there is anything wrong, please feel free to raise them. First, let's look at some function prototypes of this family:

// Input data from the keyboard to the variable

    1. IntScanf (Char* Format ,...);

// Input data from string to variable, which is the same

    1. IntSscanf (Const Char* Buffer,Const Char* Format ,...);
  1. Int_ Sscanf_l (Const Char* Buffer,Const Char* Format, locale_t locale ,...);
  2. IntSwscanf (Const Wchar_t* Buffer,Const Wchar_t* Format ,...);
  3. Int_ Swscanf_l (Const Wchar_t* Buffer,Const Wchar_t* Format, locale_t locale ,...);

Next we will only look at the standard form of sscanf functions:

    1. IntSscanf (Const Char* Buffer,Const Char* Format ,...);

Let's talk about its return value. library functions almost all return values. Some may be very strange. How do few people use sscanf's return values? Sscanf returns the number of successfully received variables. For example, sscanf ("3.14159", "% F", & PI); the return value is 1.

 

The test is as follows:Program:

    1. # include
    2. int main ()
    3. {
    4. int A;
    5. printf ( "% d" , scanf ( "% d \ n" , & ));
    6. return 0;
    7. }

If you press enter at the beginning, the program will continue waiting for you to enter, because scanf will skip the blank characters when entering numbers. In the C programming language, scanf actually uses getchar () to accept a string consisting of digits and convert it to a number. If I enter Ctrl-z (CTRL-D on UNIX),-1 is returned (depending on the compiler ). this is actually the value of the constant EOF, that is, the returned EOF. If you do not enter a number, the return value is 0. But what if I enter a floating point number? In the example above, 1 will be returned, but the buffer will leave garbage. If it is scanf ("% d", & A, & B);, an error will occur. This can be done by using a library function fflush (stdin) to clear the buffer. However, it seems that this usage is not standard in the rain. K & R, but the behavior is not defined, but we can use while (C = getchar ())! = '\ N' & C! = EOF); it can also clear the following junk scanf format matching is relatively simple, it must be remembered that the common variables must be added, otherwise the compiler cannot detect errors, however, the running process is certainly a segment error.

  1. When there are too many other users, there are too many other users, too many other users.
  2. Code │ ┃
  3. ┠ ── ─ ┼ ── ─
  4. Percent % A │ read floating point value (c99 only) percent
  5. Percent % A │ read floating point value (c99 only) percent
  6. Bytes % C │ read single character bytes
  7. Percent % d │ read decimal integer percent
  8. Percent % I │ read decimal, octal, and hexadecimal integer percent
  9. Percent % E │ read floating point number percent
  10. Percent % E │ read floating point number percent
  11. Percent % F │ read floating point number percent
  12. Percent % F │ read floating point number (c99 only) percent
  13. Limit % G │ read Floating Point Number Limit
  14. Limit % G │ read Floating Point Number Limit
  15. Bytes % O │ read eight bytes
  16. Bytes % s │ read string bytes
  17. Percent % x │ read hexadecimal number percent
  18. Percent % x │ read hexadecimal number percent
  19. Limit % P │ read pointer value limit
  20. Characters % N │ the number of equivalent characters that have been read so far
  21. Percent % u │ read unsigned decimal integer percent
  22. Combined % [] │ scan Character Set
  23. Percent % │ read % sign (percent) percent
  24. When there are too many other users, there are too many other users, too many other users.

The preceding steps are very simple. % P and % N are rarely used. skip this step. To enter %, you must add another % in front of it. Let's focus on % s and % []. % S is to read an array. The difference between % s and gets is that % s will end with any null character, while gets will end with a carriage return. You can add a number before % s to indicate the number of read-only instances. The ansi c standard adds a new feature to scanf () called scan set ). The scan set defines a character set combination that can be read by scanf () and assigned to the corresponding character array. A scan set is defined by a string of characters in square brackets. It must be prefixed with a percent sign before the left square brackets. For example, the following scan set causes scanf () to read characters A, B, and C:

    1. % [ABC]

When using the scanning set, scanf () continuously eats the characters in the set and puts them in the corresponding character array until it finds that the characters are not in the Set (that is, the scanning set only reads matching characters ). When returned, the array contains a string ending with null and composed of read characters. For many implementations, a hyphen can be used to specify a range. For example, the following scan set allows scanf () to accept letters A to Z:

    1. % [A-Z]

It is important to note that the scan set is case sensitive. Therefore, if you want to scan uppercase and lowercase characters, you must specify uppercase and lowercase letters respectively. For % [], you can also use ^ + any character (including EOF) to end the string input. For example, % [^ EOF] is not aborted until there is an EOF input. But remember thatC LanguageIt is a buffer input. Even if you % [^ A], it is impossible to end the number of A inputs before you press Enter. % S will skip the white space, but % C will not. This is

    1. Scanf ("% D", & H );
    2. Scanf ("% C", & C );

If this is written, the variable C must be the carriage return. If you want to implement this input, you can add a getchar () between the two statements. He can eat the carriage return and scanf ("% d % C", & H, & C). Enter a number and add a space. But do not use scanf ("% d \ n", & H); K & R to make it clear that any non-formatted characters must be exactly matched. This means that it is valid to add \ n after the input number. There is also * before any item, indicating that the item does not match the value.

Http://www.zxbc.cn/html/20081126/68331.html

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.