String Format Numbers Thousands 123K, Millions 123M, Billions 123B

String Format Numbers Thousands 123K, Millions 123M, Billions 123B

There are different ways to achieve this, but for me the easiest and quickest is to use the "," custom specifier

double value = 1234567890;

// Displays 1,234,567,890
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));

// Displays 1,234,568K
Console.WriteLine(value.ToString("#,##0,K", CultureInfo.InvariantCulture));

// Displays 1,235M
Console.WriteLine(value.ToString("#,##0,,M", CultureInfo.InvariantCulture));

// Displays 1B
Console.WriteLine(value.ToString("#,##0,,,B", CultureInfo.InvariantCulture));

String format numbers to millions, thousands with rounding

This should help, combined with one of the formatting techniques in the other questions you've linked to.

  internal long MaxThreeSignificantDigits(long x)
{
int i = (int)Math.Log10(x);
i = Math.Max(0, i - 2);
i = (int)Math.Pow(10, i);
return x / i * i;
}

EDIT:

OK, how about this?

 Console.WriteLine(SO30180672.FormatNumber(1));
Console.WriteLine(SO30180672.FormatNumber(12));
Console.WriteLine(SO30180672.FormatNumber(123));
Console.WriteLine(SO30180672.FormatNumber(1234));
Console.WriteLine(SO30180672.FormatNumber(12345));
Console.WriteLine(SO30180672.FormatNumber(123456));
Console.WriteLine(SO30180672.FormatNumber(1234567));
Console.WriteLine(SO30180672.FormatNumber(12345678));
Console.WriteLine(SO30180672.FormatNumber(123456789));

Following is partially copied from here: https://stackoverflow.com/a/23384710/253938

   internal class SO30180672
{
internal static string FormatNumber(long num)
{
num = MaxThreeSignificantDigits(num);

if (num >= 100000000)
return (num / 1000000D).ToString("0.#M");
if (num >= 1000000)
return (num / 1000000D).ToString("0.##M");
if (num >= 100000)
return (num / 1000D).ToString("0k");
if (num >= 100000)
return (num / 1000D).ToString("0.#k");
if (num >= 1000)
return (num / 1000D).ToString("0.##k");
return num.ToString("#,0");
}

internal static long MaxThreeSignificantDigits(long x)
{
int i = (int)Math.Log10(x);
i = Math.Max(0, i - 2);
i = (int)Math.Pow(10, i);
return x / i * i;
}
}

EDIT 2 - thank you very much to @Rhexis

   internal class SO30180672
{
internal static void RunTest()
{
Console.WriteLine(FormatNumber(1));
Console.WriteLine(FormatNumber(10));
Console.WriteLine(FormatNumber(100));
Console.WriteLine(FormatNumber(1000));
Console.WriteLine(FormatNumber(10000));
Console.WriteLine(FormatNumber(100000));
Console.WriteLine(FormatNumber(125000));
Console.WriteLine(FormatNumber(125900));
Console.WriteLine(FormatNumber(1000000));
Console.WriteLine(FormatNumber(1250000));
Console.WriteLine(FormatNumber(1258000));
Console.WriteLine(FormatNumber(10000000));
Console.WriteLine(FormatNumber(10500000));
Console.WriteLine(FormatNumber(100000000));
Console.WriteLine(FormatNumber(100100000));
}

private static string FormatNumber(long num)
{
// Ensure number has max 3 significant digits (no rounding up can happen)
long i = (long)Math.Pow(10, (int)Math.Max(0, Math.Log10(num) - 2));
num = num / i * i;

if (num >= 1000000000)
return (num / 1000000000D).ToString("0.##") + "B";
if (num >= 1000000)
return (num / 1000000D).ToString("0.##") + "M";
if (num >= 1000)
return (num / 1000D).ToString("0.##") + "K";

return num.ToString("#,0");
}
}

Custom Number Format for Thousands, Millions, Billions, AND Trillions

not possible. this "internal" formatting is by default able to work with only 3 types of numbers:

  • positive (1, 2, 5, 10, ...)
  • zero (0)
  • negative (-3, -9, -7, ...)

this can be somehow tweaked to show custom formatting like K, B, M but you always got only 3 slots you can use, meaning that you can't have trillions as the 4th type/slot

however, this would cover your needs:

=ARRAYFORMULA(IF(ABS(A:A)<10^3, A:A&"", 
IF(1*ABS(A:A)<10^6, TEXT(A:A/10^3, "#.0\k"),
IF(1*ABS(A:A)<10^9, TEXT(A:A/10^6, "#.0\M"),
IF(1*ABS(A:A)<10^12, TEXT(A:A/10^9, "#.0\B"),
IF(1*ABS(A:A)<10^15, TEXT(A:A/10^12, "#.0\T"),
IF(1*ABS(A:A)<10^18, TEXT(A:A/10^15, "#.0\Q\a"),
IF(1*ABS(A:A)<10^21, TEXT(A:A/10^18, "#.0\Q\i"),
IF(1*ABS(A:A)<10^24, TEXT(A:A/10^21, "#.0\S\x"),
IF(1*ABS(A:A)<10^27, TEXT(A:A/10^24, "#.0\S\p"),
IF(1*ABS(A:A)<10^30, TEXT(A:A/10^27, "#.0\O"),
IF(1*ABS(A:A)<10^33, TEXT(A:A/10^30, "#.0\N"),
IF(1*ABS(A:A)<10^36, TEXT(A:A/10^33, "#.0\D"),
IF(1*ABS(A:A)<10^39, TEXT(A:A/10^36, "#.0\U"),
IF(1*ABS(A:A)<10^42, TEXT(A:A/10^39, "#.0\D\d"),
IF(1*ABS(A:A)<10^45, TEXT(A:A/10^42, "#.0\T\d"),
IF(1*ABS(A:A)<10^48, TEXT(A:A/10^45, "#.0\Q\a\d"),
IF(1*ABS(A:A)<10^51, TEXT(A:A/10^48, "#.0\Q\u\d"),
IF(1*ABS(A:A)<10^54, TEXT(A:A/10^51, "#.0\S\x\d"),
IF(1*ABS(A:A)<10^57, TEXT(A:A/10^54, "#.0\S\p\d"),
IF(1*ABS(A:A)<10^60, TEXT(A:A/10^57, "#.0\O\d"),
IF(1*ABS(A:A)<10^63, TEXT(A:A/10^60, "#.0\N\d"),
IF(1*ABS(A:A)<10^66, TEXT(A:A/10^63, "#.0\V"),
IF(1*ABS(A:A)<10^69, TEXT(A:A/10^66, "#.0\C"), ))))))))))))))))))))))))

Sample Image

Sample Image

How to go about formatting 1200 to 1.2k in java

Here is a solution that works for any long value and that I find quite readable (the core logic is done in the bottom three lines of the format method).

It leverages TreeMap to find the appropriate suffix. It is surprisingly more efficient than a previous solution I wrote that was using arrays and was more difficult to read.

private static final NavigableMap<Long, String> suffixes = new TreeMap<> ();
static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
}

public static String format(long value) {
//Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
if (value < 0) return "-" + format(-value);
if (value < 1000) return Long.toString(value); //deal with easy case

Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();

long truncated = value / (divideBy / 10); //the number part of the output times 10
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}


Test code

public static void main(String args[]) {
long[] numbers = {0, 5, 999, 1_000, -5_821, 10_500, -101_800, 2_000_000, -7_800_000, 92_150_000, 123_200_000, 9_999_999, 999_999_999_999_999_999L, 1_230_000_000_000_000L, Long.MIN_VALUE, Long.MAX_VALUE};
String[] expected = {"0", "5", "999", "1k", "-5.8k", "10k", "-101k", "2M", "-7.8M", "92M", "123M", "9.9M", "999P", "1.2P", "-9.2E", "9.2E"};
for (int i = 0; i < numbers.length; i++) {
long n = numbers[i];
String formatted = format(n);
System.out.println(n + " => " + formatted);
if (!formatted.equals(expected[i])) throw new AssertionError("Expected: " + expected[i] + " but found: " + formatted);
}
}

String Format for Thousand separators

This works for $52.2, using the , number scaling specifier:

string.Format("{0:$0,.0}", 52152);

If you really want 52.1, you’ll probably have to do it “manually”; sorry. All custom formatting strings seem to round.

Reducing a BigInteger value in C#

Although Falco's answer does work, it doesn't work for what was requested. This was the solution I was looking for and received some help from a friend on it. This solution will go until there are no more prefixes left in your string array of prefixes. If you do run out of bounds, the exception will be thrown and handled by returning "Infinity".

This solution is better due to the fact there is no crunch down to doubles/decimals within this process. This solution does not have a number cap, only limit is the amount of prefixes you make/provide.

public static string ShortenBigInt(BigInteger moneyValue)
{
if (moneyValue < 1000)
return "" + moneyValue;

try
{
string moneyAsString = moneyValue.ToString();
string prefix = prefixes[(moneyAsString.Length - 1) / 3];

BigInteger chopAmmount = (moneyAsString.Length - 1) % 3 + 1;

int insertPoint = (int)chopAmmount;

chopAmmount += 2;

moneyAsString = moneyAsString.Remove(Math.Min(moneyAsString.Length - 1, (int)chopAmmount));
moneyAsString = moneyAsString.Insert(insertPoint, ".");

return moneyAsString + " " + prefix;
}
catch (Exception exceptionToBeThrown)
{
return "Infinity";
}
}


Related Topics



Leave a reply



Submit