당신이 문제를 해결하기 위해 PHP로 할 수있는 일이다
function string_to_float($foo){
if($foo*1==$foo && !empty($foo)){
return $foo*1;
} else {
return $foo;
}
}
string_to_float($row->some_decimal_field);
진짜 문제는, 왜 진수 문자열의 형식을 변환해야합니까? 수학에서 사용하려는 경우 PHP가 자동으로 변환을 수행합니다. 10 진수 문자열은 동일한 값을 가진 부동 소수점과 같습니다.
$foo = "1.2";
$bar = 1.2;
if($foo===$bar){
$equivalent = "is";
} else {
$equivalent = "is not";
}
print '"1.2" '.$equivalent.' equal to 1.2 in type and value<br />';
if($foo==$bar){
$equivalent = "is";
} else {
$equivalent = "is not";
}
print '"1.2" '.$equivalent.' equal to 1.2 in value<br />';
$foo = "1.2"*1;
$bar = 1.2;
if($foo===$bar){
$equivalent = "is";
} else {
$equivalent = "is not";
}
print '"1.2"*1 '.$equivalent.' equal to 1.2 in type and value<br />';
반환합니다 : 당신이 정상 연산자를 사용하여 문자열에 어떤 수학을한다면 당신은 부동 소수점 데이터 유형으로 고정 소수점 데이터 유형을 캐스팅 될
"1.2" is not equal to 1.2 in type and value
"1.2" is equal to 1.2 in value
"1.2"*1 is equal to 1.2 in type and value
다음은 간단한 테스트입니다. 위에서 언급 한 것처럼 GMP를 사용하지 않으면 이상한 결과 나 이상한 숫자의 근사치를 얻을 수 있습니다. –
네가 옳은 대답일지도 모르지만, 다른 사람들이 그렇게하기를 원하는 사람들을 도울 수있는 대답이다. – amaster