Yii Framework Forum: Cjavascript::encode Float Data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cjavascript::encode Float Data

#1 User is offline   Meng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 02-July 13

Posted 08 July 2013 - 01:47 AM

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
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,138
  • Joined: 17-January 09
  • Location:Russia

Posted 08 July 2013 - 12:44 PM

Fixed. Will be included in 1.1.14.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users