Skip to content

Commit

Permalink
Fix decimal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aimenux committed Dec 24, 2023
1 parent e3da6ef commit f87c2ef
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>QrCodeCli-UserSecrets</UserSecretsId>
<Version>0.0.4-preview</Version>
<Version>0.0.5-preview</Version>
<Authors>Aymen TROUDI</Authors>
<PackAsTool>true</PackAsTool>
<PackageId>QrCodeCli</PackageId>
Expand Down
14 changes: 13 additions & 1 deletion src/App/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace App.Extensions;
using System.Globalization;

namespace App.Extensions;

public static class StringExtensions
{
Expand All @@ -21,4 +23,14 @@ public static bool IsValidCoordinate(this string input)
{
return !string.IsNullOrWhiteSpace(input) && input.All(c => char.IsDigit(c) || c == '.');
}

public static bool IsDecimal(this string val, out decimal amount)
{
return decimal.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out amount);
}

public static decimal ToDecimal(this string val)
{
return decimal.Parse(val, NumberStyles.Any, CultureInfo.InvariantCulture);
}
}
4 changes: 2 additions & 2 deletions src/App/Extensions/ValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public static IRuleBuilderOptions<T, string> CoordinateIsValid<T>(this IRuleBuil
public static IRuleBuilderOptions<T, string> AmountIsValid<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder
.Must<T, string>((Func<T, string, bool>) ((x, val) => decimal.TryParse(val, out _)))
.Must<T, string>((Func<T, string, bool>) ((x, val) => val.IsDecimal(out _)))
.WithMessage("Amount '{PropertyValue}' is not valid.");
}

public static IRuleBuilderOptions<T, string> AmountIsBetween<T>(this IRuleBuilder<T, string> ruleBuilder, decimal min, decimal max)
{
return ruleBuilder
.Must<T, string>((Func<T, string, bool>) ((x, val) => decimal.TryParse(val, out var amount) && amount >= min && amount <= max))
.Must<T, string>((Func<T, string, bool>) ((x, val) => val.IsDecimal(out var amount) && amount >= min && amount <= max))
.WithMessage("Amount '{PropertyValue}' is not between '{min}' and '{max}'.");
}

Expand Down
5 changes: 3 additions & 2 deletions src/App/Services/QrCode/QrCodeFormatsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using QRCoder;
using App.Extensions;
using QRCoder;

namespace App.Services.QrCode;

Expand All @@ -24,7 +25,7 @@ string encoding
iban,
bic,
name,
decimal.Parse(amount),
amount.ToDecimal(),
information,
remittanceType,
purpose,
Expand Down
27 changes: 27 additions & 0 deletions test/Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,31 @@ public void Should_Not_Be_Equals(string left, string right)
// assert
areEquals.Should().BeFalse();
}

[Theory]
[InlineData("10.23")]
[InlineData("10,23")]
public void Should_Be_Decimal(string val)
{
// arrange
// act
var isDecimal = val.IsDecimal(out var _);

// assert
isDecimal.Should().BeTrue();
}

[Theory]
[InlineData("1abc")]
[InlineData("10;23")]
[InlineData("10..23")]
public void Should_Not_Be_Decimal(string val)
{
// arrange
// act
var isDecimal = val.IsDecimal(out var _);

// assert
isDecimal.Should().BeFalse();
}
}

0 comments on commit f87c2ef

Please sign in to comment.