[英]PHP remove trailing zero based on variable number
我想根據某些選項,省略PHP中數字的尾隨零:
decimals
=通常要返回的重要小數位數
removeTrailingZero
= true / false:刪除或不尾隨零
minDecimalsIfZero
=在removeTrailingZero
= true時使用
預期的例子:
echo get_formatted_number(123.5670, array("decimals" => 2));
// returned: 123.57
echo get_formatted_number(123, array("decimals" => 2));
// returned: 123.00
echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => false));
// returned: 123.00000000
echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => true));
// returned: 123
echo get_formatted_number(123, array("decimals" => 8, "removeTrailingZero" => true, "minDecimalsIfZero" => 2));
// returned: 123.00
echo get_formatted_number(123.4657, array("decimals" => 8, "removeTrailingZero" => true, "minDecimalsIfZero" => 2));
// returned: 123.4657 <-- in this case i'd like to return this cause i've removed the trailing zero and number of decimals (4) is bigger than minDecimalsIfZero (2): ignore decimals option (8)
echo get_formatted_number(123.4657, array("decimals" => 8, "removeTrailingZero" => false, "minDecimalsIfZero" => 2));
// returned: 123.46570000 <-- in this case i'd like to return this cause removeTrailingZero is false
這是我實際要修改的功能。
function get_formatted_number($number, $options = array()) {
if ( $number === "" ) {
return "";
}
if ( $number === NULL ) {
return "";
}
if ( isset($options['decimals']) ) {
$decimals = (int)$options['decimals'] != "" ? (int)$options['decimals'] : 2;
} else {
$decimals = 2; //default = 2
}
$value = number_format($number, $decimals);
return $value;
}
如果我理解正確,這應該可以滿足您的需求。 請參閱注釋:
function get_formatted_number($number, array $options = []): string
{
// Default values
static $DEFAULT_DECIMALS = 2;
static $DEFAULT_REMOVE_TRAILING_ZEROS = false;
static $DEFAULT_TRAILING_ZEROS_DECIMALS = 0;
// Retrieve/sanitize options
$decimals = array_key_exists('decimals', $options)
? max($DEFAULT_DECIMALS, (int)$options['decimals'])
: $DEFAULT_DECIMALS;
$remove_trailing_zeros = array_key_exists('removeTrailingZero', $options)
? !!$options['removeTrailingZero']
: $DEFAULT_REMOVE_TRAILING_ZEROS;
$trailing_zeros_decimals = array_key_exists('minDecimalsIfZero', $options)
? max($DEFAULT_TRAILING_ZEROS_DECIMALS, (int)$options['minDecimalsIfZero'])
: $DEFAULT_TRAILING_ZEROS_DECIMALS;
// Set number of decimals from options
$formatted_number = number_format($number, $decimals);
// If option is set, remove trailing zeros, keeping an optional minimum
if ($remove_trailing_zeros) {
$formatted_number = (float)$formatted_number;
if ($trailing_zeros_decimals >= strlen(substr(strrchr($formatted_number, '.'), 1))) {
$formatted_number = number_format($formatted_number, $trailing_zeros_decimals);
}
}
return $formatted_number;
}
演示在這里: https : //3v4l.org/7eqBe
找到了可能的解決方案:
function get_formatted_number($number, $options = array()) {
if ( $number === "" ) {
return "";
}
if ( $number === NULL ) {
return "";
}
if ( isset($opzioni['removeTrailingZero']) ) {
$removeTrailingZero= (bool)$opzioni['removeTrailingZero'];
} else {
$removeTrailingZero= false;
}
if ( isset($options['decimals']) ) {
$decimals = (int)$options['decimals'] != "" ? (int)$options['decimals'] : 2;
} else {
$decimals = 2; //default = 2
}
if ( isset($opzioni['minDecimalsIfZero']) ) {
$minDecimalsIfZero= $opzioni['minDecimalsIfZero'];
} else {
$minDecimalsIfZero = 2; //default
}
$value = number_format($number, $decimals);
if ( $removeTrailingZero) {
$value = preg_replace('/0{0,'.($decimals-$minDecimalsIfZero).'}$/', '', $value);
}
return $value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.