@@ -5,6 +5,27 @@ public class ValueTypeTest
55
66 #region Constants & Statics
77
8+ public static void EffectiveLength_4 ( )
9+ {
10+ var a = 999999.9999 ;
11+ var b = 999999999.9999 ;
12+ var c = 999999999999.9999 ;
13+ var e = 999999999999999.9999 ;
14+ var f = 999999999999999999.9999 ;
15+
16+ var ee = 999999999999999.9999m ;
17+ var ff = 999999999999999999.9999m ;
18+
19+ Console . WriteLine ( $ "double 6+4 { a } ") ;
20+ Console . WriteLine ( $ "double 9+4 { b } ") ;
21+ Console . WriteLine ( $ "double 12+4 { c } ") ;
22+ Console . WriteLine ( $ "double 15+4 { e } ") ; // 无效,15 位整数部分 + 4 位小数
23+ Console . WriteLine ( $ "double 18+4 { f } ") ; // 无效,18 位整数部分 + 4 位小数
24+
25+ Console . WriteLine ( $ "decimal 15+4 { ee } ") ;
26+ Console . WriteLine ( $ "decimal 18+4 { ff } ") ;
27+ }
28+
829 public static void OutOfPrecisionFalseTest ( )
930 {
1031 //(15 - 16 位有效数字)
@@ -19,14 +40,26 @@ public static void OutOfPrecisionFalseTest()
1940 public static void OutOfPrecisionTrueTest ( )
2041 {
2142 //(15 - 16 位有效数字)
22- var maxValue = 1_999_999_999_999.9999 ;
23- var beyondMax = 2_000_000_000_000 ; // 超过 12 位整数部分 + 4 位小数
43+ var maxValue = 1_999_999_999_999.9999 ; // 超过 12 位整数部分 + 4 位小数
44+ var beyondMax = 2_000_000_000_000 ;
2445
2546 Console . WriteLine ( $ "Max value with 4 decimals: { maxValue } ") ;
2647 Console . WriteLine ( $ "Beyond max value with 4 decimals: { beyondMax } ") ;
2748 Console . WriteLine ( $ "Are maxValue and beyondMax equal? { maxValue == beyondMax } ") ;
2849 }
2950
51+ public static void RoundAlgorithm ( )
52+ {
53+ // 银行家舍入法(四舍六入五成双)
54+ var rounded = Math . Round ( 1.235m , 2 , MidpointRounding . ToEven ) ;
55+
56+ // 会计常用舍入(总是远离零)
57+ var accountingRound = Math . Round ( 1.235m , 2 , MidpointRounding . AwayFromZero ) ;
58+
59+ Console . WriteLine ( rounded ) ;
60+ Console . WriteLine ( accountingRound ) ;
61+ }
62+
3063 public static void ShowSize ( )
3164 {
3265 Console . WriteLine ( $ "sbyte: Min={ sbyte . MinValue } , Max={ sbyte . MaxValue } , Size={ sizeof ( sbyte ) } bytes") ;
0 commit comments