99// Redistribution and use in source and binary forms with or without
1010// modifications are permitted.
1111
12+ #nullable enable
13+
1214using System ;
1315using System . Numerics ;
1416
@@ -17,25 +19,25 @@ namespace Neo
1719 /// <summary>
1820 /// Represents a fixed-point number of arbitrary precision.
1921 /// </summary>
20- public struct BigDecimal : IComparable < BigDecimal > , IEquatable < BigDecimal >
22+ public readonly struct BigDecimal : IComparable < BigDecimal > , IEquatable < BigDecimal >
2123 {
22- private readonly BigInteger value ;
23- private readonly byte decimals ;
24+ private readonly BigInteger _value ;
25+ private readonly byte _decimals ;
2426
2527 /// <summary>
2628 /// The <see cref="BigInteger"/> value of the number.
2729 /// </summary>
28- public readonly BigInteger Value => value ;
30+ public readonly BigInteger Value => _value ;
2931
3032 /// <summary>
3133 /// The number of decimal places for this number.
3234 /// </summary>
33- public readonly byte Decimals => decimals ;
35+ public readonly byte Decimals => _decimals ;
3436
3537 /// <summary>
3638 /// The sign of the number.
3739 /// </summary>
38- public readonly int Sign => value . Sign ;
40+ public readonly int Sign => _value . Sign ;
3941
4042 /// <summary>
4143 /// Initializes a new instance of the <see cref="BigDecimal"/> struct.
@@ -44,8 +46,8 @@ public struct BigDecimal : IComparable<BigDecimal>, IEquatable<BigDecimal>
4446 /// <param name="decimals">The number of decimal places for this number.</param>
4547 public BigDecimal ( BigInteger value , byte decimals )
4648 {
47- this . value = value ;
48- this . decimals = decimals ;
49+ _value = value ;
50+ _decimals = decimals ;
4951 }
5052
5153 /// <summary>
@@ -59,9 +61,9 @@ public unsafe BigDecimal(decimal value)
5961 fixed ( int * p = span )
6062 {
6163 ReadOnlySpan < byte > buffer = new ( p , 16 ) ;
62- this . value = new BigInteger ( buffer [ ..12 ] , isUnsigned : true ) ;
63- if ( buffer [ 15 ] != 0 ) this . value = - this . value ;
64- decimals = buffer [ 14 ] ;
64+ _value = new BigInteger ( buffer [ ..12 ] , isUnsigned : true ) ;
65+ if ( buffer [ 15 ] != 0 ) _value = - _value ;
66+ _decimals = buffer [ 14 ] ;
6567 }
6668 }
6769
@@ -77,15 +79,15 @@ public unsafe BigDecimal(decimal value, byte decimals)
7779 fixed ( int * p = span )
7880 {
7981 ReadOnlySpan < byte > buffer = new ( p , 16 ) ;
80- this . value = new BigInteger ( buffer [ ..12 ] , isUnsigned : true ) ;
82+ _value = new BigInteger ( buffer [ ..12 ] , isUnsigned : true ) ;
8183 if ( buffer [ 14 ] > decimals )
8284 throw new ArgumentException ( null , nameof ( value ) ) ;
8385 else if ( buffer [ 14 ] < decimals )
84- this . value *= BigInteger . Pow ( 10 , decimals - buffer [ 14 ] ) ;
86+ _value *= BigInteger . Pow ( 10 , decimals - buffer [ 14 ] ) ;
8587 if ( buffer [ 15 ] != 0 )
86- this . value = - this . value ;
88+ _value = - _value ;
8789 }
88- this . decimals = decimals ;
90+ _decimals = decimals ;
8991 }
9092
9193 /// <summary>
@@ -95,16 +97,16 @@ public unsafe BigDecimal(decimal value, byte decimals)
9597 /// <returns>The <see cref="BigDecimal"/> that has the new number of decimal places.</returns>
9698 public readonly BigDecimal ChangeDecimals ( byte decimals )
9799 {
98- if ( this . decimals == decimals ) return this ;
100+ if ( _decimals == decimals ) return this ;
99101 BigInteger value ;
100- if ( this . decimals < decimals )
102+ if ( _decimals < decimals )
101103 {
102- value = this . value * BigInteger . Pow ( 10 , decimals - this . decimals ) ;
104+ value = _value * BigInteger . Pow ( 10 , decimals - _decimals ) ;
103105 }
104106 else
105107 {
106- BigInteger divisor = BigInteger . Pow ( 10 , this . decimals - decimals ) ;
107- value = BigInteger . DivRem ( this . value , divisor , out BigInteger remainder ) ;
108+ var divisor = BigInteger . Pow ( 10 , _decimals - decimals ) ;
109+ value = BigInteger . DivRem ( _value , divisor , out var remainder ) ;
108110 if ( remainder > BigInteger . Zero )
109111 throw new ArgumentOutOfRangeException ( nameof ( decimals ) ) ;
110112 }
@@ -120,7 +122,7 @@ public readonly BigDecimal ChangeDecimals(byte decimals)
120122 /// <exception cref="FormatException"><paramref name="s"/> is not in the correct format.</exception>
121123 public static BigDecimal Parse ( string s , byte decimals )
122124 {
123- if ( ! TryParse ( s , decimals , out BigDecimal result ) )
125+ if ( ! TryParse ( s , decimals , out var result ) )
124126 throw new FormatException ( ) ;
125127 return result ;
126128 }
@@ -131,10 +133,10 @@ public static BigDecimal Parse(string s, byte decimals)
131133 /// <returns>The <see cref="string"/> representing the number.</returns>
132134 public override readonly string ToString ( )
133135 {
134- BigInteger divisor = BigInteger . Pow ( 10 , decimals ) ;
135- BigInteger result = BigInteger . DivRem ( value , divisor , out BigInteger remainder ) ;
136+ var divisor = BigInteger . Pow ( 10 , _decimals ) ;
137+ var result = BigInteger . DivRem ( _value , divisor , out var remainder ) ;
136138 if ( remainder == 0 ) return result . ToString ( ) ;
137- return $ "{ result } .{ remainder . ToString ( "d" + decimals ) } ". TrimEnd ( '0' ) ;
139+ return $ "{ result } .{ remainder . ToString ( "d" + _decimals ) } ". TrimEnd ( '0' ) ;
138140 }
139141
140142 /// <summary>
@@ -146,17 +148,17 @@ public override readonly string ToString()
146148 /// <returns><see langword="true"/> if a number is successfully parsed; otherwise, <see langword="false"/>.</returns>
147149 public static bool TryParse ( string s , byte decimals , out BigDecimal result )
148150 {
149- int e = 0 ;
150- int index = s . IndexOfAny ( new [ ] { 'e' , 'E' } ) ;
151+ var e = 0 ;
152+ var index = s . IndexOfAny ( [ 'e' , 'E' ] ) ;
151153 if ( index >= 0 )
152154 {
153- if ( ! sbyte . TryParse ( s [ ( index + 1 ) ..] , out sbyte e_temp ) )
155+ if ( ! sbyte . TryParse ( s [ ( index + 1 ) ..] , out var e_temp ) )
154156 {
155157 result = default ;
156158 return false ;
157159 }
158160 e = e_temp ;
159- s = s . Substring ( 0 , index ) ;
161+ s = s [ .. index ] ;
160162 }
161163 index = s . IndexOf ( '.' ) ;
162164 if ( index >= 0 )
@@ -165,15 +167,15 @@ public static bool TryParse(string s, byte decimals, out BigDecimal result)
165167 e -= s . Length - index - 1 ;
166168 s = s . Remove ( index , 1 ) ;
167169 }
168- int ds = e + decimals ;
170+ var ds = e + decimals ;
169171 if ( ds < 0 )
170172 {
171173 result = default ;
172174 return false ;
173175 }
174176 if ( ds > 0 )
175177 s += new string ( '0' , ds ) ;
176- if ( ! BigInteger . TryParse ( s , out BigInteger value ) )
178+ if ( ! BigInteger . TryParse ( s , out var value ) )
177179 {
178180 result = default ;
179181 return false ;
@@ -184,15 +186,15 @@ public static bool TryParse(string s, byte decimals, out BigDecimal result)
184186
185187 public readonly int CompareTo ( BigDecimal other )
186188 {
187- BigInteger left = value , right = other . value ;
188- if ( decimals < other . decimals )
189- left *= BigInteger . Pow ( 10 , other . decimals - decimals ) ;
190- else if ( decimals > other . decimals )
191- right *= BigInteger . Pow ( 10 , decimals - other . decimals ) ;
189+ BigInteger left = _value , right = other . _value ;
190+ if ( _decimals < other . _decimals )
191+ left *= BigInteger . Pow ( 10 , other . _decimals - _decimals ) ;
192+ else if ( _decimals > other . _decimals )
193+ right *= BigInteger . Pow ( 10 , _decimals - other . _decimals ) ;
192194 return left . CompareTo ( right ) ;
193195 }
194196
195- public override readonly bool Equals ( object obj )
197+ public override readonly bool Equals ( object ? obj )
196198 {
197199 if ( obj is not BigDecimal @decimal ) return false ;
198200 return Equals ( @decimal ) ;
@@ -205,8 +207,8 @@ public readonly bool Equals(BigDecimal other)
205207
206208 public override readonly int GetHashCode ( )
207209 {
208- BigInteger divisor = BigInteger . Pow ( 10 , decimals ) ;
209- BigInteger result = BigInteger . DivRem ( value , divisor , out BigInteger remainder ) ;
210+ var divisor = BigInteger . Pow ( 10 , _decimals ) ;
211+ var result = BigInteger . DivRem ( _value , divisor , out var remainder ) ;
210212 return HashCode . Combine ( result , remainder ) ;
211213 }
212214
0 commit comments