简体   繁体   English

PHP JSON编码浮点数

[英]php JSON encode float number

it seems a simple thing: I would like to encode: 似乎很简单:我想编码:

echo json_encode(array('lol' => 104564563225.1));

gives: 得到:

{"lol":1.0e+11}

I would like to be the output: 我想成为输出:

{"lol":104564563225.1} 

How can this be achieved? 如何做到这一点? some more server data just in case: 一些其他服务器数据,以防万一:

PHP Version 5.6.0
System  Windows NT XXXXXXXXXXXXXX 6.3 build 9200 (Windows Server 2012 R2
Standard Edition) i586
Build Date  Aug 27 2014 11:49:46
Compiler    MSVC11 (Visual C++ 2012)
Architecture    x86
Configure Command   cscript /nologo configure.js "--enable-snapshot-   build" "--enable-debug-pack" "--disable-zts" "--disable-isapi" "--disable-nsapi" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" 
"--with-pdo-oci=c:\php-sdk\oracle\x86\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-sdk\oracle\x86\instantclient_12_1\sdk,shared" "--with-enchant=shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--without-analyzer" "--with-pgo"
Server API  CGI/FastCGI
Virtual Directory Support   disabled
Configuration File (php.ini) Path   C:\Windows
Loaded Configuration File   C:\Program Files (x86)\PHP\v5.6\php.ini 

The string representation of floating point numbers depends on the precision configuration (see its documentation on php.ini directives). 浮点数的字符串表示形式取决于precision配置(请参阅有关php.ini指令的文档)。

Also, echo(104564563225.1); 另外, echo(104564563225.1); should display the same value as json_encode() produces. 应该显示与json_encode()产生的值相同的值。

By default the precision is 14 and with this value your code displays: 默认情况下, precision14 ,使用此值,您的代码将显示:

{"lol":104564563225.1}

Maybe it has a lower value on your system. 也许它在您的系统上具有较低的价值。

You can get its current value from your script using ini_get() : 您可以使用ini_get()从脚本中获取其当前值:

echo(ini_get('precision'));

and you can change it using ini_set() : 您可以使用ini_set()进行更改:

ini_set('precision', '14');

Of course, searching and changing it in your php.ini is another option, if it's available to you. 当然,如果可用,在php.ini搜索和更改它也是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM