Skip to content

Commit

Permalink
Import NumberAbbreviationExtensions (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored Mar 19, 2024
1 parent 7f9fe54 commit 6bbc7e3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
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);
}
}
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);
}
}

0 comments on commit 6bbc7e3

Please sign in to comment.