关于php的number_format函数的问题.
echo number_format(88899626759579787, 0, '', '');
为什么输出结果是88899626759579792 ?
回复讨论(解决方案)
我也是新手,问了下度娘,她说,格式出错了!
出自 http://www.w3school.com.cn/php/func_string_number_format.asp
因为你的环境应该32位,要表示你上述88899626759579787的数字,要用浮点型,而浮点型的精度是不没有办法保证的.
可以通过这个例子就知道了
echo PHP_INT_MAX; // php的整形能表示的最大整数var_dump(number_format(88899626759579700, 0, '', ''));
发帖的时候犹豫要不要写环境来着的.
开发环境是 win7 64bit, 生产环境是 centOS 64bit的.
还是同样错误.
有解决方法吗?
这个是从别的api娶过来的数字, 直接显示的话会显示科技计数法.
用的strval函数也不好用.
谢谢!
因为你的环境应该32位,要表示你上述88899626759579787的数字,要用浮点型,而浮点型的精度是不没有办法保证的.
可以通过这个例子就知道了
echo PHP_INT_MAX; // php的整形能表示的最大整数var_dump(number_format(88899626759579700, 0, '', ''));
另外. api返回来的是json数据. 我用json_decode(), 好像自动转换成数字类型.
还有php用的也是64位版本的..
有没有试过按 #1 说的改下看看
有没有试过按 #1 说的改下看看
16位数的是好使的.
越界了,php 的 long 类型无法表示那么大的正数
php 开发组织从未发行过 for win 的 64位 版本
如果有条件的话,最好能升级到php5.4+,因为php在5.4的时候,对json_decode增加一个参数
5.4.0 The options parameter was added.
Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
<?php$json = '12345678901234567890';var_dump(json_decode($json));var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));?>以上例程会输出:float(1.2345678901235E+19)string(20) "12345678901234567890"
如果不能升级,建议首先将超过int整形长度的全数字文本首先加一个特殊标识,例如
{"a":"12345678901234567890"}
可以通过正则替换成
{"a":"__12345678901234567890"}
等json_decode完了以后,接着将__标识去掉
如果有条件的话,最好能升级到php5.4+,因为php在5.4的时候,对json_decode增加一个参数
5.4.0 The options parameter was added.
Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
<?php$json = '12345678901234567890';var_dump(json_decode($json));var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));?>以上例程会输出:float(1.2345678901235E+19)string(20) "12345678901234567890"
如果不能升级,建议首先将超过int整形长度的全数字文本首先加一个特殊标识,例如
{"a":"12345678901234567890"}
可以通过正则替换成
{"a":"__12345678901234567890"}
等json_decode完了以后,接着将__标识去掉
谢谢详细的回复.
已经解决了. 和如果不能升级的方法类似.
只是在开发环境取过来的数据是越界的, 在实际生产环境当中不会有越界的问题.
所以就字符串解析方法解决了.
再次感谢!
结贴!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
