Format Numbers With Million (M) and Billion (B) Suffixes

Format numbers with million (M) and billion (B) suffixes

If you begin with this numeric vector x,

x <- c(6e+06, 75000400, 743450000, 340000, 4300000)

you could do the following.

paste(format(round(x / 1e6, 1), trim = TRUE), "M")
# [1] "6.0 M" "75.0 M" "743.5 M" "0.3 M" "4.3 M"

And if you're not concerned about trailing zeros, just remove the format() call.

paste(round(x / 1e6, 1), "M")
# [1] "6 M" "75 M" "743.5 M" "0.3 M" "4.3 M"

Alternatively, you could assign an S3 class with print method and keep y as numeric underneath. Here I use paste0() to make the result a bit more legible.

print.million <- function(x, quote = FALSE, ...) {
x <- paste0(round(x / 1e6, 1), "M")
NextMethod(x, quote = quote, ...)
}
## assign the 'million' class to 'x'
class(x) <- "million"
x
# [1] 6M 75M 743.5M 0.3M 4.3M
x[]
# [1] 6000000 75000400 743450000 340000 4300000

You could do the same for billions and trillions as well. For information on how to put this into a data frame, see this answer, as you'll need both a format() and an as.data.frame() method.

Format numbers with M (million) suffixes if a given cell is greater than 100,000

This function will convert a numeric value to that format, then just use sapply to apply it to each column of the data.frame

milfun <- function(x) {ifelse(x>1e5,paste0(round(x/1e6,3),"M"),x)}
d$total <- sapply(d$total,milfun)
d$se <- sapply(d$se,milfun)
d
ID total se
1 a 145M 9M
2 b 9.5 0.84
3 c 0.867M 0.121M

note you had 145000000=145M, not 1.45M in your desired response.

How to convert number into K thousands M million and B billion suffix in jsp

Adapting the answer from over here it should look something like

public static String withSuffix(long count) {
if (count < 1000) return "" + count;
int exp = (int) (Math.log(count) / Math.log(1000));
return String.format("%.1f %c",
count / Math.pow(1000, exp),
"kMGTPE".charAt(exp-1));
}

Test code:

for (long num : new long[] { 0, 27, 999, 1000, 110592,
28991029248L, 9223372036854775807L })
System.out.printf("%20d: %8s%n", num, withSuffix(num));

Output:

                   0:        0
27: 27
999: 999
1000: 1.0 k
110592: 110.6 k
28991029248: 29.0 G
9223372036854775807: 9.2 E

Excel custom formatting positive/negative numbers with Thousand/Million/Billion (K/M/B) suffixes

Since it appears to not be possible with a one-stop solution (which while I think doing this without a one-stop is a little messy, but I also understand why they can't just magically understand every conceivable custom format iteration), I am opting for a two-step approach:

I will have 3 custom formats. One for the positive numbers with suffixes, another for the negative numbers with suffixes, and a third that is just the "standard" positive/negative number format (displayed in the question). I will then use a series of two or three conditional formatting rules to determine which of these custom formats will be displayed.

Personally, I am going to use the +/- format as the cell's format, then apply two conditional rules that change it to the two suffix variations, but I could see the argument for using conditional formats for all three.

Thanks for the feedback and the reminder that conditional formatting exists to aid with this very kind of issue.

Format a number as 2.5K if a thousand or more, otherwise 900

Sounds like this should work for you:

function kFormatter(num) {    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)}    console.log(kFormatter(1200)); // 1.2kconsole.log(kFormatter(-1200)); // -1.2kconsole.log(kFormatter(900)); // 900console.log(kFormatter(-900)); // -900

Parse currency values from CSV, convert numerical suffixes for Million and Billion

We could use gsubfn to replace the 'B', 'M' with 'e+9', 'e+6' and convert to numeric (as.numeric).

is.na(v1) <- v1=='N/A'
options(scipen=999)
library(gsubfn)
as.numeric(gsubfn('([A-Z]|\\$)', list(B='e+9', M='e+6',"$"=""),v1))
#[1] 1200000 3100000000 NA

EDIT: Modified based on @nicola's suggestion

data

v1 <- c('$1.2M', '$3.1B', 'N/A')

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");
}
}


Related Topics



Leave a reply



Submit