Convert Number into Xx.Xx Million Format

Convert number into xx.xx million format?

Try this from, https://php.net/manual/en/function.number-format.php#89888:

<?php 
function nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",", "", $n));

// is this a number?
if (!is_numeric($n)) return false;

// now filter it;
if ($n > 1000000000000) return round(($n/1000000000000), 2).' trillion';
elseif ($n > 1000000000) return round(($n/1000000000), 2).' billion';
elseif ($n > 1000000) return round(($n/1000000), 2).' million';
elseif ($n > 1000) return round(($n/1000), 2).' thousand';

return number_format($n);
}

echo nice_number('14120000'); //14.12 million

?>

Convert price type to xxx milion xxx thousand

This code:

$number = 25400055;

$millionsRemainder = $number % 1000000;

$millions = ($number - $millionsRemainder) / 1000000;

$thousandsRemainder = $millionsRemainder % 1000;

$thousands = ($millionsRemainder - $thousandsRemainder) / 1000;

echo $millions . ' million ' . $thousands . ' thousands ' . ' and ' . $thousandsRemainder;

Should output "25 million 400 thousands and 55"

Number format for post view count

You can't really do that right out of the box.

There's a neat function that will help you achieve what you want:

<?php
# Output easy-to-read numbers
# by james at bandit.co.nz
function bd_nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));

// is this a number?
if(!is_numeric($n)) return false;

// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
else if($n>1000000000) return round(($n/1000000000),1).' billion';
else if($n>1000000) return round(($n/1000000),1).' million';
else if($n>1000) return round(($n/1000),1).' thousand';

return number_format($n);
}
?>

Source: http://php.net/manual/en/function.number-format.php#89888

How to make a good number format?

Here is function i wrote for you:

function my_number_format($number, $thousands_separator=',') {
static $words = array(null, null/*'thousand'*/, 'million', 'billion', 'trillion' , /*etc*/);
$parts = explode(',', number_format($number, 0, null, ','));
$index = count($parts)-1;
if($words[$index]){
$divider = pow(1000, $index);
$result = round($number/$divider, 3);
$result = str_replace('.', $thousands_separator, $result).' '.$wordss[$index];
} else {
$result = join($thousands_separator, $parts);
}
return $result;
}

Same function in short syntax:

function my_number_format($number, $thousands_separator=',') {
static $words = array(null, null/*'thousand'*/, 'million', 'billion', 'trillion' , /*etc*/);
$parts = explode(',', number_format($number, 0, null, ','));
$index = count($parts)-1;
return $words[$index]
? str_replace('.', $thousands_separator, round($number/pow(1000,$index),3)).' '.$words[$index]
: join($thousands_separator, $parts);
}

Test:

echo my_number_format(1);           // 1
echo my_number_format(10); // 10
echo my_number_format(100); // 100
echo my_number_format(1000); // 1,000
echo my_number_format(10000); // 10,000
echo my_number_format(100000); // 100,000
echo my_number_format(999999); // 999,999

echo my_number_format(1000000); // 1 million
echo my_number_format(1260000); // 1,26 million
echo my_number_format(234500000); // 234,5 million

echo my_number_format(1000000000); // 1 billion
echo my_number_format(1781000000); // 1,781 billion
echo my_number_format(12781000000); // 12,781 billion

Convert a large scale characters to date-format-like characters in r

Go with sub.

date <- c("20051023", "20151023")
sub("^(\\d{4})(\\d{2})(\\d{2})$", "\\1-\\2-\\3", date)
# [1] "2005-10-23" "2015-10-23"

Oracle Apex - Format number in millions

Not declaratively, as far as I can tell. But, you can do it yourself (perhaps).

  • col is original value
  • c1 is that number (col) divided by a million
  • c2 rounds the result to the 2nd decimal (which is OK, but - decimals are missing (see 1)
  • c3 uses to_char function
  • final_result is c3 concatenated with an M

Note that col, c1 and c2 are numbers, while c3 and final_result are strings.

SQL> with test (col) as
2 (select 1000000 from dual union all
3 select 1234567 from dual union all
4 select 1234567890 from dual
5 )
6 select col,
7 col/1e6 c1,
8 round(col/1e6, 2) c2,
9 to_char(col/1e6, 'fm99990D00') c3,
10 --
11 to_char(col/1e6, 'fm99990D00') || 'M' final_result
12 from test;

COL C1 C2 C3 FINAL_RESULT
---------- ---------- ---------- --------- ---------------
1000000 1 1 1,00 1,00M
1234567 1,234567 1,23 1,23 1,23M
1234567890 1234,56789 1234,57 1234,57 1234,57M

SQL>

Format long number to shorter version in Lua

Pattern matching doesn't seem like the right direction for this problem.

Assuming 2 digits after decimal point are kept in the shorter version, try:

function foo(n)
if n >= 10^6 then
return string.format("%.2fm", n / 10^6)
elseif n >= 10^3 then
return string.format("%.2fk", n / 10^3)
else
return tostring(n)
end
end

Test:

print(foo(17478))
print(foo(2832))
print(foo(1548034))

Output:

17.48k
2.83k
1.55m

Formating Comment Count in WordPress with PHP

Actually above code is working fine and output is 1.266M

Hard coded example:

$number = 1265788;
if ($number < 1000000) {
// Anything less than a million
$n_format = number_format($number);
echo $n_format;
} else if ($number < 1000000000) {
// Anything less than a billion
$n_format = number_format($number / 1000000, 3) . 'M';
echo $n_format;
} else {
// At least a billion
$n_format = number_format($number / 1000000000, 3) . 'B';
echo $n_format;
}

Dynamic:

$comments_count = wp_count_comments();
$number = $comments_count->total_comments;
if ($number < 1000000) {
// Anything less than a million
$n_format = number_format($number);
echo $n_format;
} else if ($number < 1000000000) {
// Anything less than a billion
$n_format = number_format($number / 1000000, 3) . 'M';
echo $n_format;
} else {
// At least a billion
$n_format = number_format($number / 1000000000, 3) . 'B';
echo $n_format;
}


Related Topics



Leave a reply



Submit