Skip to content

Commit b7d6044

Browse files
authored
Merge pull request #44 from shinji-san/fix-LuhnCheckDigitCalculation
Fix - Luhn check digit calculation Resolves: #44
2 parents 90d9317 + af657de commit b7d6044

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Fixed
9+
- Fixed a bug in `Luhn.ComputeLuhnNumber` and `Luhn.ComputeLuhnCheckDigit` methods that sometimes returned an incorrect result.
10+
711
## [1.0.0] - 2024-02-19
812
### Added
913
- Added .NET 8 support

src/Luhn.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static byte ComputeLuhnCheckDigit(ReadOnlySpan<char> number) =>
7373
#else
7474
public static byte ComputeLuhnCheckDigit(string number) =>
7575
#endif
76-
(byte)(Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits());
76+
(byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
7777

7878
/// <summary>
7979
/// Computes the Luhn number which is a combination of the given number and the calculated check digit.
@@ -89,7 +89,7 @@ public static string ComputeLuhnNumber(ReadOnlySpan<char> number)
8989
public static string ComputeLuhnNumber(string number)
9090
#endif
9191
{
92-
byte checkDigit = (byte)(Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits());
92+
byte checkDigit = (byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
9393
#if NET6_0_OR_GREATER
9494
return string.Concat(number.Trim(), checkDigit.ToString(CultureInfo.InvariantCulture));
9595
#else

tests/LuhnTest.cs

+12
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ public class LuhnTest
1616
public static IEnumerable<object[]> LuhnCheckDigitComputeSet =>
1717
new List<object[]>
1818
{
19+
new object[] { 4, "1789372997" },
20+
new object[] { 0, "353011133330000" },
1921
new object[] { 3, "7992739871" },
2022
new object[] { 3, "07992739871" },
2123
new object[] { 3, "007992739871" },
2224
new object[] { 2, "110494" },
25+
new object[] { 7, "1893" },
2326
new object[] { 5, "37828224631000" },
2427
new object[] { 5, "637095000000000" },
2528
new object[] { 5, " 637095000000000" },
@@ -35,10 +38,13 @@ public class LuhnTest
3538
public static IEnumerable<object[]> LuhnNumberComputeSet =>
3639
new List<object[]>
3740
{
41+
new object[] { "17893729974", "1789372997" },
42+
new object[] { "3530111333300000", "353011133330000" },
3843
new object[] { "79927398713", "7992739871" },
3944
new object[] { "079927398713", "07992739871" },
4045
new object[] { "0079927398713", "007992739871" },
4146
new object[] { "1104942", "110494" },
47+
new object[] { "18937", "1893" },
4248
new object[] { "378282246310005", "37828224631000" },
4349
new object[] { "6370950000000005", "637095000000000" },
4450
new object[] { "6370950000000005", " 637095000000000" },
@@ -54,10 +60,13 @@ public class LuhnTest
5460
public static IEnumerable<object[]> LuhnNumberValidationSet =>
5561
new List<object[]>
5662
{
63+
new object[] { true, "17893729974" },
64+
new object[] { true, "3530111333300000" },
5765
new object[] { true, "79927398713" },
5866
new object[] { true, "0079927398713" },
5967
new object[] { true, "079927398713" },
6068
new object[] { true, "1104942" },
69+
new object[] { true, "18937" },
6170
new object[] { true, "01104942" },
6271
new object[] { true, "378282246310005" },
6372
new object[] { true, "6370950000000005" },
@@ -79,11 +88,14 @@ public class LuhnTest
7988
public static IEnumerable<object[]> LuhnCheckDigitValidationSet =>
8089
new List<object[]>
8190
{
91+
new object[] { true, "1789372997", 4 },
92+
new object[] { true, "353011133330000", 0 },
8293
new object[] { true, "7992739871", 3 },
8394
new object[] { true, "07992739871", 3 },
8495
new object[] { true, "007992739871", 3 },
8596
new object[] { true, "0007992739871", 3 },
8697
new object[] { true, "110494", 2 },
98+
new object[] { true, "1893", 7 },
8799
new object[] { true, "37828224631000", 5 },
88100
new object[] { true, "637095000000000", 5 },
89101
new object[] { true, " 637095000000000", 5 },

0 commit comments

Comments
 (0)