Числовой формат PHP европейский формат (= 3.213.124,4355) без 0 в десятичном виде

Я хочу преобразовать числа в PHP следующим образом:

100000 -> 100.000
3213 -> 3.213
54523.321 -> 54.523,321
42324.00 -> 42.324
3412.1 -> 3.412,1

Поэтому я хочу иметь. как разделитель тысяч и как десятичный разделитель, и я не хочу бесполезного 0 в десятичном виде. Как я могу это сделать?

Я знаю, что могу использовать float, чтобы избавиться от 0 после десятичного числа. И я знаю, что могу использовать number_format для замены разделителя тысяч / десятичных дробей, но в этом случае вам также нужно определить количество десятичных дробей, и вы получите 0 в десятичной дроби …

-4

Решение

С помощью number_format() следующим образом:

$number = 1234.5600;
$nb = number_format($number, 2, ',', '.'); // 1.234,56

Он автоматически удалит все нули в конце десятичных дробей.

5

Другие решения

Нашел ответ сам (с использованием / изменением кода, который я нашел в комментариях к number_format () ):

function number_format_unchanged_precision($number, $dec_point='.', $thousands_sep=','){
if($dec_point==$thousands_sep){
trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
// It corresponds "PHP Warning:  Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
}
$decimals = strlen(substr(strrchr($number, "."), 1));

return number_format($number, $decimals, $dec_point, $thousands_sep);
}

и здесь также функция, которая включает в себя округление, но не добавляет бесполезные 0: (я думаю, что нормальная функция number_format () должна работать следующим образом …)

function number_format_without_zeroindecimal($number, $maxdecimal, $dec_point='.', $thousands_sep=','){
if($dec_point==$thousands_sep){
trigger_error('2 parameters for ' . __METHOD__ . '() have the same value, that is "' . $dec_point . '" for $dec_point and $thousands_sep', E_USER_WARNING);
// It corresponds "PHP Warning:  Wrong parameter count for number_format()", which occurs when you use $dec_point without $thousands_sep to number_format().
}
$decimals = strlen(substr(strrchr(round($number,$maxdecimal), "."), 1));

return number_format($number, $decimals, $dec_point, $thousands_sep);
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector