-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f9fe54
commit 2a41fe3
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Tingle.Extensions.Primitives/Extensions/NumberAbbreviationExtensions.cs
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,61 @@ | ||
using System.Globalization; | ||
|
||
namespace System; | ||
|
||
/// <summary> | ||
/// Extensions for shortening numbers | ||
/// </summary> | ||
public static class NumberAbbreviationExtensions | ||
{ | ||
private const string FormatBillions = "0,,,.###B"; | ||
private const string FormatMillions = "0,,.##M"; | ||
private const string FormatKilo = "0,.#K"; | ||
|
||
/// <summary> | ||
/// Converts the numeric value of this instance to its equivalent string representation. | ||
/// </summary> | ||
/// <param name="source">the numeric value</param> | ||
/// <returns>The string representation of the value of this instance as specified by format and provider.</returns> | ||
/// <exception cref="FormatException">format is invalid or not supported.</exception> | ||
public static string ToStringAbbreviated(this long source) => source.ToStringAbbreviated(CultureInfo.InvariantCulture); | ||
|
||
/// <summary> | ||
/// Converts the numeric value of this instance to its equivalent string representation | ||
/// using the specified culture-specific format information. | ||
/// </summary> | ||
/// <param name="source">the numeric value</param> | ||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param> | ||
/// <returns>The string representation of the value of this instance as specified by format and provider.</returns> | ||
/// <exception cref="FormatException">format is invalid or not supported.</exception> | ||
public static string ToStringAbbreviated(this long source, IFormatProvider formatProvider) | ||
{ | ||
if (source > 999999999 || source < -999999999) return source.ToString(FormatBillions, formatProvider); | ||
else if (source > 999999 || source < -999999) return source.ToString(FormatMillions, formatProvider); | ||
else if (source > 999 || source < -999) return source.ToString(FormatKilo, formatProvider); | ||
return source.ToString(formatProvider); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the numeric value of this instance to its equivalent string representation. | ||
/// </summary> | ||
/// <param name="source">the numeric value</param> | ||
/// <returns>The string representation of the value of this instance as specified by format and provider.</returns> | ||
/// <exception cref="FormatException">format is invalid or not supported.</exception> | ||
public static string ToStringAbbreviated(this int source) => source.ToStringAbbreviated(CultureInfo.InvariantCulture); | ||
|
||
/// <summary> | ||
/// Converts the numeric value of this instance to its equivalent string representation | ||
/// using the specified culture-specific format information. | ||
/// </summary> | ||
/// <param name="source">the numeric value</param> | ||
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param> | ||
/// <returns>The string representation of the value of this instance as specified by format and provider.</returns> | ||
/// <exception cref="FormatException">format is invalid or not supported.</exception> | ||
public static string ToStringAbbreviated(this int source, IFormatProvider formatProvider) | ||
{ | ||
if (source > 999999999 || source < -999999999) return source.ToString(FormatBillions, formatProvider); | ||
else if (source > 999999 || source < -999999) return source.ToString(FormatMillions, formatProvider); | ||
else if (source > 999 || source < -999) return source.ToString(FormatKilo, formatProvider); | ||
return source.ToString(formatProvider); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/Tingle.Extensions.Primitives.Tests/Extensions/NumberAbbreviationExtensionsTests.cs
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,39 @@ | ||
namespace Tingle.Extensions.Modeling.Tests; | ||
|
||
public class NumberAbbreviationExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData(1_001, "1K")] | ||
[InlineData(1_010, "1K")] | ||
[InlineData(10_300, "10.3K")] | ||
[InlineData(3_900_120, "3.9M")] | ||
[InlineData(3_910_120, "3.91M")] | ||
[InlineData(3_000_120, "3M")] | ||
[InlineData(1_400_000_120, "1.4B")] | ||
[InlineData(1_000_000_120, "1B")] | ||
[InlineData(1_004_000_120, "1.004B")] | ||
[InlineData(1_044_000_120, "1.044B")] | ||
public void ToStringAbbreviated_Int(int original, string excepected) | ||
{ | ||
var actual = original.ToStringAbbreviated(); | ||
Assert.Equal(excepected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1_001, "1K")] | ||
[InlineData(1_010, "1K")] | ||
[InlineData(10_300, "10.3K")] | ||
[InlineData(3_900_120, "3.9M")] | ||
[InlineData(3_910_120, "3.91M")] | ||
[InlineData(3_000_120, "3M")] | ||
[InlineData(1_400_000_120, "1.4B")] | ||
[InlineData(1_000_000_120, "1B")] | ||
[InlineData(1_004_000_120, "1.004B")] | ||
[InlineData(1_044_000_120, "1.044B")] | ||
[InlineData(10_044_000_120, "10.044B")] | ||
public void ToStringAbbreviated_Long(long original, string excepected) | ||
{ | ||
var actual = original.ToStringAbbreviated(); | ||
Assert.Equal(excepected, actual); | ||
} | ||
} |