php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69255 Double to Int convertation while json encode-decode
Submitted: 2015-03-18 09:57 UTC Modified: 2015-03-18 21:21 UTC
From: oleksiy dot tykhonovskyy at agito dot pl Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.5.22 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: oleksiy dot tykhonovskyy at agito dot pl
New email:
PHP Version: OS:

 

 [2015-03-18 09:57 UTC] oleksiy dot tykhonovskyy at agito dot pl
Description:
------------
Double to Int convertation while json encode-decode in case number has .0 end... 

---
From manual page: http://www.php.net/function.json-encode
---


Test script:
---------------
php > $a = ['price' => 11.00];
php > var_dump($a);
array(1) {
  'price' =>
  double(11)
}
php > var_dump(json_decode(json_encode($a), true));
array(1) {
  'price' =>
  int(11)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-18 21:21 UTC] cmb@php.net
-Status: Open +Status: Not a bug
 [2015-03-18 21:21 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The JSON specifications don't distinguish between integer and
floating point numbers (or other representations), therefore 11.0
and 11 are considered identical, so json_encode() chooses the
shortest representation.

However, there will be a new option to json_encode(), 
JSON_PRESERVE_ZERO_FRACTION, in PHP 7[1].

[1] <https://wiki.php.net/rfc/json_preserve_fractional_part>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC