numberformater php format to k and m

Solutions on MaxInterview for numberformater php format to k and m by the best coders in the world

showing results for - "numberformater php format to k and m"
Milo
09 Oct 2018
1
2<?php
3$fmt new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
4echo $fmt->formatCurrency(1234567.891234567890000"EUR")."\n";
5echo $fmt->formatCurrency(1234567.891234567890000"RUR")."\n";
6$fmt new NumberFormatter( 'ru_RU', NumberFormatter::CURRENCY );
7echo $fmt->formatCurrency(1234567.891234567890000"EUR")."\n";
8echo $fmt->formatCurrency(1234567.891234567890000"RUR")."\n";
9?>
10
11
Claude
15 Oct 2019
1/**
2 * @param $n
3 * @return string
4 * Use to convert large positive numbers in to short form like 1K+, 100K+, 199K+, 1M+, 10M+, 1B+ etc
5 */
6function number_format_short( $n ) {
7	if ($n > 0 && $n < 1000) {
8		// 1 - 999
9		$n_format = floor($n);
10		$suffix = '';
11	} else if ($n >= 1000 && $n < 1000000) {
12		// 1k-999k
13		$n_format = floor($n / 1000);
14		$suffix = 'K+';
15	} else if ($n >= 1000000 && $n < 1000000000) {
16		// 1m-999m
17		$n_format = floor($n / 1000000);
18		$suffix = 'M+';
19	} else if ($n >= 1000000000 && $n < 1000000000000) {
20		// 1b-999b
21		$n_format = floor($n / 1000000000);
22		$suffix = 'B+';
23	} else if ($n >= 1000000000000) {
24		// 1t+
25		$n_format = floor($n / 1000000000000);
26		$suffix = 'T+';
27	}
28
29	return !empty($n_format . $suffix) ? $n_format . $suffix : 0;
30}
31
María Camila
12 Jan 2018
1function shortNumber($num) 
2{
3    $units = ['', 'K', 'M', 'B', 'T'];
4    for ($i = 0; $num >= 1000; $i++) {
5        $num /= 1000;
6    }
7    return round($num, 1) . $units[$i];
8}