php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79148 json_encode json_preserve_zero_fraction parameter only display one zero
Submitted: 2020-01-21 12:45 UTC Modified: 2020-01-23 13:38 UTC
From: chinaearl at gmail dot com Assigned:
Status: Wont fix Package: JSON related
PHP Version: 7.3.13 OS: Centos
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chinaearl at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-21 12:45 UTC] chinaearl at gmail dot com
Description:
------------
json_encode json_preserve_zero_fraction parameter only display one zero

json_encode([5.00], JSON_PRESERVE_ZERO_FRACTION);
display: 5.0
=============================================================
Hope display: 5.00

Test script:
---------------
json_encode json_preserve_zero_fraction parameter only display one zero

json_encode([5.00], JSON_PRESERVE_ZERO_FRACTION);
display: 5.0
=============================================================
Hope display: 5.00

Expected result:
----------------
json_encode json_preserve_zero_fraction parameter only display one zero

json_encode([5.00], JSON_PRESERVE_ZERO_FRACTION);
display: 5.0
=============================================================
Hope display: 5.00

Actual result:
--------------
json_encode json_preserve_zero_fraction parameter only display one zero

json_encode([5.00], JSON_PRESERVE_ZERO_FRACTION);
display: 5.0
=============================================================
Hope display: 5.00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-21 15:48 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2020-01-21 15:48 UTC] cmb@php.net
> Hope display: 5.00

Why?  JSON_PRESERVE_ZERO_FRACTION is meant to enforce floating
point representation (i.e. not integer); isn't that sufficient
for data exchange?
 [2020-01-21 16:09 UTC] chinaearl at gmail dot com
$arr = [
   'a' => (float)1.00
 ];
echo json_encode($arr,JSON_PRESERVE_ZERO_FRACTION);

output:{"a":1.0}

I want output: {"a":1.00}
 [2020-01-21 16:12 UTC] cmb@php.net
-Status: Feedback +Status: Open
 [2020-01-21 16:12 UTC] cmb@php.net
But why would you want that output?  For pretty printing?
 [2020-01-21 16:12 UTC] cmb@php.net
-Status: Assigned +Status: Open -Assigned To: cmb +Assigned To:
 [2020-01-21 16:37 UTC] chinaearl at gmail dot com
Yes, Business needs.
 [2020-01-21 16:39 UTC] chinaearl at gmail dot com
I'm thinking, should add another one, JSON_PRESERVE_DOUBLE_ZERO_FRACTION
^_^
 [2020-01-21 18:59 UTC] rowan dot collins at gmail dot com
There is no way for your example to give the output you want. When you write "$a = 5.00;", PHP doesn't store anywhere the fact that you typed two zeroes, it just stores the number 5 in a floating point variable. Similarly, when you call json_encode([5.00]), PHP is just passing the floating point number 5 to the function json_encode. There is no way for the function to know that you wanted two trailing zeroes on this particular 5, but one or three or nineteen trailing zeroes on other 5s.

A JSON_PRESERVE_DOUBLE_ZERO_FRACTION constant would at least be possible, but it seems oddly specific - why not also JSON_PRESERVE_TRIPLE_ZERO_FRACTION, JSON_PRESERVE_QUADRUPLE_ZERO_FRACTION, and so on? 

Note that adding a single zero is not just a matter of presentation - other code parsing the resulting JSON can see that the value "5.0" was intended as a floating point number, and "5" as an integer. The difference between "5.00" and "5.0", however, would only matter to human readers.
 [2020-01-22 02:25 UTC] chinaearl at gmail dot com
No matter how the bottom layer is designed, but I have encountered this problem in actual development, it is necessary for business, enter 5.00, json_enocde shows 5.00
 [2020-01-23 13:38 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: json +Package: JSON related
 [2020-01-23 13:38 UTC] nikic@php.net
If you need to preserve the exact formatting of the number, store it as a string.

Alternatively and more likely what you want to do, format it with two zeros when printing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 17:01:29 2024 UTC