Do câu hỏi này là cũ. Đầu tiên, tôi xin lỗi về điều này.
Câu hỏi là về số xxx.xx nhưng trong trường hợp đó là x, xxx.xxxxx hoặc phân tách thập phân khác biệt như xxxx, xxxx, điều này có thể khó tìm và loại bỏ các chữ số 0 khỏi giá trị thập phân.
/**
* Remove zero digits [include zero trails - 123.450, 123.000] from decimal value.
*
* @param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* @param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* @return string Return removed zero digits from decimal value. Only return as string!
*/
function removeZeroDigitsFromDecimal[$number, $decimal_sep = '.']
{
$explode_num = explode[$decimal_sep, $number];
if [is_countable[$explode_num] && count[$explode_num] > 1] {
// if exploded number is more than 1 [Example: explode with . for nnnn.nnn is 2]
// replace `is_countable[]` with `is_array[]` if you are using PHP older than 7.3.
$explode_num[count[$explode_num]-1] = preg_replace['/[0+]$/', '', $explode_num[count[$explode_num]-1]];
if [$explode_num[count[$explode_num]-1] === ''] {
// if the decimal value is now empty.
// unset it to prevent nnn. without any number.
unset[$explode_num[count[$explode_num]-1]];
}
$number = implode[$decimal_sep, $explode_num];
}
unset[$explode_num];
return [string] $number;
}
Và đây là mã để kiểm tra.
$tests = [
1234 => 1234,
-1234 => -1234,
'12,345.67890' => '12,345.6789',
'-12,345,678.901234' => '-12,345,678.901234',
'12345.000000' => '12345',
'-12345.000000' => '-12345',
'12,345.000000' => '12,345',
'-12,345.000000000' => '-12,345',
];
foreach [$tests as $number => $assert] {
$result = removeZeroDigitsFromDecimal[$number];
assert[$result === [string] $assert, new \Exception[$result . ' [' . gettype[$result] . '] is not matched ' . $assert . ' [' . gettype[$assert] . ']']];
echo $number . ' => ' . [string] $assert . '
';
}
echo '' . PHP_EOL;
$tests = [
1234 => 1234,
-1234 => -1234,
'12.345,67890' => '12.345,6789',
'-12.345.678,901234' => '-12.345.678,901234',
'12345,000000' => '12345',
'-12345,000000' => '-12345',
'-12.345,000000000' => '-12.345',
'-12.345,000000,000' => '-12.345,000000',// this is correct assertion. Weird ,000000,000 but only last 000 will be removed.
];
foreach [$tests as $number => $assert] {
$result = removeZeroDigitsFromDecimal[$number, ','];
assert[$result === [string] $assert, new \Exception[$result . ' [' . gettype[$result] . '] is not matched ' . $assert . ' [' . gettype[$assert] . ']']];
echo $number . ' => ' . [string] $assert . '
';
}
Tất cả các bài kiểm tra nên được thông qua và không có lỗi.
Tại sao '-12.345,000000,000'
sẽ là '-12.345,000000'
không phải '-12.345'
? Bởi vì hàm này dành cho việc loại bỏ các chữ số bằng 0 [bao gồm các đường mòn bằng không] khỏi giá trị thập phân. Nó không xác nhận cho định dạng số chính xác. Đó phải là một chức năng khác.
Because this function is for remove zero digits [include zero trails]
from decimal value. It is not validation for the correct number format. That should be another function.
Tại sao luôn trả về chuỗi? Bởi vì tốt hơn là sử dụng tính toán với các hàm bcxxx
hoặc sử dụng với số lượng lớn.
Because it is better to use in calculation with bcxxx
functions, or use with big number.
12 tháng 5 năm 2010
Tôi không thể tìm thấy một chức năng number clean
đẹp cho PHP để loại bỏ các số không dẫn đầu và dấu vết [và dấu thập phân] để nghe là chức năng number_clean, thưởng thức :]
Phương pháp loại bỏ hoặc dải số không có số thập phân: Hoạt động trên các số: 0,02 = 0,02;4.000 = 4;80 = 80.function clean_num[ $num ]{
$pos = strpos[$num, '.'];
if[$pos === false] { // it is integer number
return $num;
}else{ // it is decimal number
return rtrim[rtrim[$num, '0'], '.'];
}
}
Làm thế nào loại bỏ các số không từ số thập phân trong PHP?
Làm thế nào để loại bỏ dấu vết bằng không trong PHP?
Làm thế nào để loại bỏ số không khỏi một số trong PHP?
Làm thế nào để loại bỏ giá trị sau thập phân trong PHP?
'; echo floor[10.5]; // Round down, this would echo 10.