I’m having a problem in Yii CJavaScript::encode() when i pass float data into it.
$output = array(54.4);
echo CJavaScript::encode($output);
i will get the result like this:
[54.3999999999999986]
after i check the source code, i found the problem is come from line 81 in CJavaScript.php:
return rtrim(sprintf('%.16F',$value),'0'); // locale-independent representation
seems like sprintf(’%.16F’,$value) has format the float to 16 decimal points.
meanwhile i also checked the CJSON::encode function, it handler the float encode in a different way. and output the data correctly. CJSON.php line 109
return str_replace(',','.',(float)$var); // locale-independent representation
i’m wondering why they are handling the float in different ways…
Yii version yii-1.1.13.
PHP Version 5.4.7
Windows XP Professional Service Pack 3