-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dalrankov/hotfix/1.0.1
Hotfix/1.0.1 Fix decimal rounding and formatting
- Loading branch information
Showing
3 changed files
with
42 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Globalization; | ||
|
||
namespace OpenMicroFiscal.Extensions; | ||
|
||
public static class DecimalExtensions | ||
{ | ||
public static decimal RoundTo(this decimal value, int decimals) | ||
{ | ||
var roundedValue = Math.Round(value, decimals, MidpointRounding.AwayFromZero); | ||
var formattedRoundedValue = roundedValue.ToFormattedString(decimals); | ||
return decimal.Parse(formattedRoundedValue, CultureInfo.InvariantCulture); | ||
} | ||
|
||
public static decimal IncreaseBy(this decimal value, decimal percentage) | ||
{ | ||
return value + value * 0.01M * percentage; | ||
} | ||
|
||
public static string ToFormattedString(this decimal value, int decimals) | ||
{ | ||
const char zeroChar = '0'; | ||
return value.ToString($"{zeroChar}.{new string(zeroChar, decimals)}", CultureInfo.InvariantCulture); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters