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