Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oivoodoo
Created March 11, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oivoodoo/ad9f4ddf99610993ab6354e0096ab8a3 to your computer and use it in GitHub Desktop.
Save oivoodoo/ad9f4ddf99610993ab6354e0096ab8a3 to your computer and use it in GitHub Desktop.
using System.Globalization;
public static class FormatNumber
{
public static string ToKMB(this decimal num)
{
if (num > 999999999 || num < -999999999)
{
return num.ToString("0,,,.###B", CultureInfo.InvariantCulture);
}
if (num > 999999 || num < -999999)
{
return num.ToString("0,,.##M", CultureInfo.InvariantCulture);
}
if (num > 999 || num < -999)
{
return num.ToString("0,.#K", CultureInfo.InvariantCulture);
}
return num.ToString(CultureInfo.InvariantCulture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment