@@ -7,112 +7,109 @@ namespace DotNetExtensions.Core;
77/// </summary>
88public static class NumberExtensions
99{
10- /// <summary>
11- /// Calculates the number of digits in the given number, in base 10.
12- /// </summary>
13- /// <typeparam name="T">The numeric type implementing <see cref="INumber{T}"/>.</typeparam>
1410 /// <param name="number">The number to analyze.</param>
15- /// <returns>The number of digits in the number.</returns>
16- /// <remarks>
17- /// For negative numbers, the sign is ignored, and the count includes all digits of the absolute value.
18- /// </remarks>
19- public static int NumberOfDigits < T > ( this T number ) where T : IBinaryInteger < T >
11+ /// <typeparam name="T">The numeric type implementing <see cref="INumber{T}"/>.</typeparam>
12+ extension < T > ( T number) where T : IBinaryInteger < T >
2013 {
21- if ( number == T . Zero )
22- return 1 ;
23-
24- var ten = T . CreateChecked ( 10 ) ;
25- var count = 0 ;
26- while ( number != T . Zero )
14+ /// <summary>
15+ /// Calculates the number of digits in the given number, in base 10.
16+ /// </summary>
17+ /// <returns>The number of digits in the number.</returns>
18+ /// <remarks>
19+ /// For negative numbers, the sign is ignored, and the count includes all digits of the absolute value.
20+ /// </remarks>
21+ public int NumberOfDigits ( )
2722 {
28- number /= ten ;
29- count ++ ;
30- }
23+ if ( number == T . Zero )
24+ return 1 ;
3125
32- return count ;
33- }
34-
35- /// <summary>
36- /// Determines whether the given number is a palindrome.
37- /// </summary>
38- /// <typeparam name="T">The numeric type implementing <see cref="IBinaryInteger{T}"/>.</typeparam>
39- /// <param name="number">The number to check.</param>
40- /// <returns><c>true</c> if the number is a palindrome; otherwise, <c>false</c>.</returns>
41- /// <remarks>
42- /// Negative numbers are not considered palindromes.
43- /// </remarks>
44- public static bool IsPalindrome < T > ( this T number ) where T : IBinaryInteger < T >
45- {
46- if ( number < T . Zero )
47- return false ;
26+ var ten = T . CreateChecked ( 10 ) ;
27+ var count = 0 ;
28+ while ( number != T . Zero )
29+ {
30+ number /= ten ;
31+ count ++ ;
32+ }
4833
49- try
50- {
51- return number == number . Reverse ( ) ;
34+ return count ;
5235 }
53- catch ( OverflowException )
36+
37+ /// <summary>
38+ /// Determines whether the given number is a palindrome.
39+ /// </summary>
40+ /// <returns><c>true</c> if the number is a palindrome; otherwise, <c>false</c>.</returns>
41+ /// <remarks>
42+ /// Negative numbers are not considered palindromes.
43+ /// </remarks>
44+ public bool IsPalindrome ( )
5445 {
55- // If the number is too large to reverse, it can't be a palindrome.
56- return false ;
57- }
58- }
46+ if ( number < T . Zero )
47+ return false ;
5948
60- /// <summary>
61- /// Reverses the digits of the given number.
62- /// </summary>
63- /// <typeparam name="T">The numeric type implementing <see cref="IBinaryInteger{T}"/>.</typeparam>
64- /// <param name="number">The number to reverse.</param>
65- /// <returns>The reversed number.</returns>
66- /// <remarks>
67- /// For single-digit numbers, the result is the number itself.
68- /// </remarks>
69- /// <exception cref="OverflowException">
70- /// Thrown if reversing the number causes an overflow.
71- /// </exception>
72- public static T Reverse < T > ( this T number ) where T : IBinaryInteger < T >
73- {
74- var ten = T . CreateChecked ( 10 ) ;
49+ try
50+ {
51+ return number == number . Reverse ( ) ;
52+ }
53+ catch ( OverflowException )
54+ {
55+ // If the number is too large to reverse, it can't be a palindrome.
56+ return false ;
57+ }
58+ }
7559
76- var reverse = T . Zero ;
77- while ( number != T . Zero )
60+ /// <summary>
61+ /// Reverses the digits of the given number.
62+ /// </summary>
63+ /// <returns>The reversed number.</returns>
64+ /// <remarks>
65+ /// For single-digit numbers, the result is the number itself.
66+ /// </remarks>
67+ /// <exception cref="OverflowException">
68+ /// Thrown if reversing the number causes an overflow.
69+ /// </exception>
70+ public T Reverse ( )
7871 {
79- checked
72+ var ten = T . CreateChecked ( 10 ) ;
73+
74+ var reverse = T . Zero ;
75+ while ( number != T . Zero )
8076 {
81- reverse = reverse * ten + number % ten ;
77+ checked
78+ {
79+ reverse = reverse * ten + number % ten ;
80+ }
81+
82+ number /= ten ;
8283 }
8384
84- number /= ten ;
85+ return reverse ;
8586 }
8687
87- return reverse ;
88- }
89-
90- /// <summary>
91- /// Splits the given number into two parts: left and right.
92- /// </summary>
93- /// <typeparam name="T">The numeric type implementing <see cref="INumber{T}"/>.</typeparam>
94- /// <param name="number">The number to split.</param>
95- /// <returns>
96- /// A tuple containing the left and right parts of the number.
97- /// </returns>
98- /// <remarks>
99- /// The number is split based on the number of digits, with the left part containing
100- /// the most significant digits and the right part containing the least significant digits.
101- /// </remarks>
102- /// <exception cref="ArgumentOutOfRangeException">
103- /// Thrown if the number is negative.
104- /// </exception>
105- public static ( T Left , T Right ) Split < T > ( this T number ) where T : IBinaryInteger < T >
106- {
107- ArgumentOutOfRangeException . ThrowIfNegative ( number ) ;
88+ /// <summary>
89+ /// Splits the given number into two parts: left and right.
90+ /// </summary>
91+ /// <returns>
92+ /// A tuple containing the left and right parts of the number.
93+ /// </returns>
94+ /// <remarks>
95+ /// The number is split based on the number of digits, with the left part containing
96+ /// the most significant digits and the right part containing the least significant digits.
97+ /// </remarks>
98+ /// <exception cref="ArgumentOutOfRangeException">
99+ /// Thrown if the number is negative.
100+ /// </exception>
101+ public ( T Left , T Right ) Split ( )
102+ {
103+ ArgumentOutOfRangeException . ThrowIfNegative ( number ) ;
108104
109- var numDigits = number . NumberOfDigits ( ) ;
110- var halfDigits = numDigits / 2 + numDigits % 2 ;
111- var divisor = T . CreateChecked ( Math . Pow ( 10 , halfDigits ) ) ;
105+ var numDigits = number . NumberOfDigits ( ) ;
106+ var halfDigits = numDigits / 2 + numDigits % 2 ;
107+ var divisor = T . CreateChecked ( Math . Pow ( 10 , halfDigits ) ) ;
112108
113- var leftPart = number / divisor ;
114- var rightPart = number % divisor ;
109+ var leftPart = number / divisor ;
110+ var rightPart = number % divisor ;
115111
116- return ( leftPart , rightPart ) ;
112+ return ( leftPart , rightPart ) ;
113+ }
117114 }
118115}
0 commit comments