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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Pull Requests

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: Sat Oct 12 09:01:27 2024 UTC