. Net (C #): It is not difficult to keep the valid number of floating point numbers as simple as C ++!

Source: Internet
Author: User

Note:

The output of C/C ++ may have different results under different compilers. If your compiler is different from mine, we recommend that you copy C/C ++ code and run it on your own compiler to view the results.

 

All the following C # code is compiled and run by Visual C #2010 Express.

The following C/C ++ code is compiled and run by Visual C ++ 2010 Express.

 

Floating Point Output of C ++: 6 Valid digits are reserved by default.

Code:

Double arr [4] = {123.4567, 12345670, 12.34567, 0.001234567 };

 

For (INT I = 0; I <4; I ++)

Cout <arr [I] <Endl;

Output:

123.457

1.23457e + 007

12.3457

0.00123457

 

C # float is 7 bits, and double is 15 bits. Refer to msdn: http://msdn.microsoft.com/zh-cn/library/dwhawy9k.aspx#GFormatString

Note that if you do not need any formatting symbols, the. NET output uses the "G" formatting character by default.

VaR arr = new double [] {123.4567, 12345670, 12.34567, 0.001234567 };

 

Foreach (var d in ARR)

Console. writeline (D. tostring ());

Output:

123.4567

12345670

12.34567

0.001234567

All numbers are output as they are.

 

The accuracy of the output stream can be set by keeping valid numbers in C ++. Of course, the setting accuracy is not limited to cout, it can be any ios_base. The ios_base.precision function can be used here. Or use the setprecision function in <iomanip>. They all achieve the same effect!

 

For example, retain four valid numbers.

Code:

Double arr [4] = {123.4567, 12345670, 12.34567, 0.001234567 };

 

Cout. Precision (4 );

For (INT I = 0; I <4; I ++)

Cout <arr [I] <Endl;

Output:

123.5

1.235e + 007

12.35

0.001235

 

If C # is used, the "G" precision indicates that the valid digits are retained.(I was surprised to find that many people did not know how to find "C # retain valid numbers" in the search engine, and there were a lot of things in csdn ...... Every day, people may be misled, or some people may think that C # is so troublesome to keep a valid number ......)

 

Retain four digits.

Code:

VaR arr = new double [] {123.4567, 12345670, 12.34567, 0.001234567 };

 

Foreach (var d in ARR)

Console. writeline (D. tostring ("G4 "));

Output:

123.5

1.235e + 07

12.35

0.001235

Related Article

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.