그래서,이 좋은 해결책인지 아닌지 정말 모르겠어요 -하지만 잘 동작하는 것 :
function number_format_en($number) {
// first remove everything execpt -,.
$cleanNumber = preg_replace('/[^\\d-,.]+/', '', $number);
$last_dot = strrpos($cleanNumber, '.');
$last_comma = strrpos($cleanNumber, ',');
if($last_dot !== false || $last_comma !== false) {
if($last_dot > $last_comma) { // decimal seperator = dot
$decimal_point = '.';
if(substr_count($cleanNumber, '.') > 1) {
// could be totaly wrong 1,234.567.890
// or there are no decimals 1.234.567.890
// removing all dots and commas and returning the value
return preg_replace('/[^\\d-]+/', '', $cleanNumber);
}
} else { // decimal seperator = comma
$decimal_point = ',';
if(substr_count($cleanNumber, ',') > 1) {
// could be totaly wrong 1.234,567,890
// or there are no decimals 1,234,567,890
// removing all dots and commas and returning the value
return preg_replace('/[^\\d-]+/', '', $cleanNumber);
}
}
} else { // no decimals
$decimal_point = false;
$decimals = 0;
}
if($decimal_point !== false) {
// if decimals are delivered, get the count of them
$length = strlen($cleanNumber);
$position = strpos($cleanNumber, $decimal_point);
$decimals = $length - $position - 1;
if($decimal_point == '.') {
// remove all commas if seperator = .
$cleanNumber = str_replace(',', '', $cleanNumber);
} elseif($decimal_point == ',') {
// remove all dots if seperator = ,
$cleanNumber = str_replace('.', '', $cleanNumber);
// now switch comma with dot
$cleanNumber = str_replace(',', '.', $cleanNumber);
}
}
return $cleanNumber;
}
내가 마지막 점이나 쉼표를 확인거야, 그리고 그 I 형식에 따라 숫자 함수는 원래 숫자와 같은 소수 자릿수를 반환합니다. 는 "알려진"버그가 있습니다 : 누군가가이 경우에 코멘트를 게시 할 수있는 경우
number_format_en('1.234.567,89') // 1234567.89
number_format_en('-1,234,567.89') // -1234567.89
number_format_en('1.234.567.89') // 123456789 (only dots)
number_format_en('1,234,567,89') // 123456789 (only commas)
좋을 텐데 : 사람이 1.000.000 또는 100처럼 전체 번호를 입력하는 경우는 예를 들어 1000000
을 반환 좋은 방법인지 아닌지 또는 더 나은 해결책을 제시하는 답변입니다.