How to Convert Strings With Billion or Million Abbreviation into Integers in a List

How to convert strings with billion or million abbreviation into integers in a list

You can use list comprehension with a dict mapping:

l = ["150M", "360M", "2.6B", "3.7B"]
m = {'K': 3, 'M': 6, 'B': 9, 'T': 12}
print([int(float(i[:-1]) * 10 ** m[i[-1]] / 1000) for i in l])

This outputs:

[150000, 360000, 2600000, 3700000]

How can I consistently convert strings like "3.71B" and "4M" to numbers in Python?

>>> from decimal import Decimal
>>> d = {
'K': 3,
'M': 6,
'B': 9
}
>>> def text_to_num(text):
if text[-1] in d:
num, magnitude = text[:-1], text[-1]
return Decimal(num) * 10 ** d[magnitude]
else:
return Decimal(text)

>>> text_to_num('3.17B')
Decimal('3170000000.00')
>>> text_to_num('4M')
Decimal('4000000')
>>> text_to_num('4.1234567891234B')
Decimal('4123456789.1234000000000')

You can int() the result if you want too

formatting long numbers as strings in python

I don't think there's a built-in function that does that. You'll have to roll your own, e.g.:

def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])

print('the answer is %s' % human_format(7436313)) # prints 'the answer is 7.44M'

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

Convert long number into abbreviated string in JavaScript, with a special shortness requirement

I believe ninjagecko's solution doesn't quite conform with the standard you wanted. The following function does:

function intToString (value) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor((""+value).length/3);
var shortValue = parseFloat((suffixNum != 0 ? (value / Math.pow(1000,suffixNum)) : value).toPrecision(2));
if (shortValue % 1 != 0) {
shortValue = shortValue.toFixed(1);
}
return shortValue+suffixes[suffixNum];
}

For values greater than 99 trillion no letter will be added, which can be easily fixed by appending to the 'suffixes' array.

Edit by Philipp follows: With the following changes it fits with all requirements perfectly!

function abbreviateNumber(value) {
var newValue = value;
if (value >= 1000) {
var suffixes = ["", "k", "m", "b","t"];
var suffixNum = Math.floor( (""+value).length/3 );
var shortValue = '';
for (var precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2) { break; }
}
if (shortValue % 1 != 0) shortValue = shortValue.toFixed(1);
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
}

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.

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

how to convert numbers to million in javascript

I have no idea what $scope.MoneyFormat is.

So I simplified your function to a plain old js function and it works.

function convertToInternationalCurrencySystem (labelValue) {

// Nine Zeroes for Billions
return Math.abs(Number(labelValue)) >= 1.0e+9

? (Math.abs(Number(labelValue)) / 1.0e+9).toFixed(2) + "B"
// Six Zeroes for Millions
: Math.abs(Number(labelValue)) >= 1.0e+6

? (Math.abs(Number(labelValue)) / 1.0e+6).toFixed(2) + "M"
// Three Zeroes for Thousands
: Math.abs(Number(labelValue)) >= 1.0e+3

? (Math.abs(Number(labelValue)) / 1.0e+3).toFixed(2) + "K"

: Math.abs(Number(labelValue));

}

alert( convertToInternationalCurrencySystem (6800000) ); // this outputs 6.8M

JSFiddle: https://jsfiddle.net/r5ju34ey/



Related Topics



Leave a reply



Submit