PHP: 创建数字的缩写, How to create the abbreviation of a number in PHP

PHP: 创建数字的缩写, How to create the abbreviation of a number in PHP
PHP: 创建数字的缩写, How to create the abbreviation of a number in PHP

 

目前,拥有大多数订户的频道拥有65119648个订阅者。可能你没有轻易阅读这个数字,所以像65M或6511K这样的缩写会更容易阅读吗?很多人真的估计这种功能的实用性,所以如果你的网站或应用程序处理的人数很长,没人想完全阅读,那就给他们看一下这个功能。

在本文中,我们将向您展示2个用PHP生成数字缩写的实现。

 

A.确切的缩写

如果您愿意准确显示提供号码的缩写,则此实现将起到作用:

<?php 

/**
 * Function that converts a numeric value into an exact abbreviation
 */
function number_format_short( $n, $precision = 1 ) {
	if ($n < 900) {
		// 0 - 900
		$n_format = number_format($n, $precision);
		$suffix = '';
	} else if ($n < 900000) {
		// 0.9k-850k
		$n_format = number_format($n / 1000, $precision);
		$suffix = 'K';
	} else if ($n < 900000000) {
		// 0.9m-850m
		$n_format = number_format($n / 1000000, $precision);
		$suffix = 'M';
	} else if ($n < 900000000000) {
		// 0.9b-850b
		$n_format = number_format($n / 1000000000, $precision);
		$suffix = 'B';
	} else {
		// 0.9t+
		$n_format = number_format($n / 1000000000000, $precision);
		$suffix = 'T';
	}
  // Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1"
  // Intentionally does not affect partials, eg "1.50" -> "1.50"
	if ( $precision > 0 ) {
		$dotzero = '.' . str_repeat( '0', $precision );
		$n_format = str_replace( $dotzero, '', $n_format );
	}
	return $n_format . $suffix;
}

 

这意味着,显示带小数的缩写,例如

<?php 

$examples = array(15, 129, 400, 1500, 14350, 30489, 50222, 103977 , 2540388, 53003839);

foreach($examples as $example){
    echo "$example => " . number_format_short($example) . "\n";
}

/*

Outputs: 
	15 => 15
	129 => 129
	400 => 400
	1500 => 1.5K
	14350 => 14.4K
	30489 => 30.5K
	50222 => 50.2K
	103977 => 104K
	2540388 => 2.5M
	53003839 => 53M
*/

此实现使用该  number_format函数,该函数格式化具有分组数千的数字。

 

B.一般缩写

如果你愿意只显示提供号码的重要部分(没有精确的千人组),这个实现就可以了:

<?php 

/**
 * Function that converts a numeric value into an abbreviation.
 */
function number_format_short( $n ) {
	if ($n > 0 && $n < 1000) {
		// 1 - 999
		$n_format = floor($n);
		$suffix = '';
	} else if ($n >= 1000 && $n < 1000000) {
		// 1k-999k
		$n_format = floor($n / 1000);
		$suffix = 'K+';
	} else if ($n >= 1000000 && $n < 1000000000) {
		// 1m-999m
		$n_format = floor($n / 1000000);
		$suffix = 'M+';
	} else if ($n >= 1000000000 && $n < 1000000000000) {
		// 1b-999b
		$n_format = floor($n / 1000000000);
		$suffix = 'B+';
	} else if ($n >= 1000000000000) {
		// 1t+
		$n_format = floor($n / 1000000000000);
		$suffix = 'T+';
	}

	return !empty($n_format . $suffix) ? $n_format . $suffix : 0;
}

 

该代码片段不会生成带有“小数”的缩写,而是添加主号码和加号,例如:

<?php

$examples = array(15, 129, 400, 1500, 14350, 30489, 50222, 103977 , 2540388, 53003839);

foreach($examples as $example){
    echo "$example => " . number_format_short($example) . "\n";
}

/*

Outputs: 
	15 => 15
    129 => 129
    400 => 400
    1500 => 1K+
    14350 => 14K+
    30489 => 30K+
    50222 => 50K+
    103977 => 103K+
    2540388 => 2M+
    53003839 => 53M+
*/

 

本文: PHP: 创建数字的缩写, How to create the abbreviation of a number in PHP

Leave a Reply