Req #79148 [Opn->Wfx]: json_encode json_preserve_zero_fraction parameter only display one zero

From: Date: Thu, 23 Jan 2020 13:38:17 +0000
Subject: Req #79148 [Opn->Wfx]: json_encode json_preserve_zero_fraction parameter only display one zero
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225065@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79148&edit=1 ID: 79148 Updated by: nikic@php.net Reported by: chinaearl at gmail dot com Summary: json_encode json_preserve_zero_fraction parameter only display one zero -Status: Open +Status: Wont fix Type: Feature/Change Request -Package: json +Package: JSON related Operating System: Centos PHP Version: 7.3.13 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2020-01-22 02:25:31] 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-21 18:59:42] 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-21 16:39:18] chinaearl at gmail dot com I'm thinking, should add another one, JSON_PRESERVE_DOUBLE_ZERO_FRACTION ^_^ ------------------------------------------------------------------------ [2020-01-21 16:37:16] chinaearl at gmail dot com Yes, Business needs. ------------------------------------------------------------------------ [2020-01-21 16:12:36] cmb@php.net But why would you want that output? For pretty printing? ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=79148 -- Edit this bug report at https://bugs.php.net/bug.php?id=79148&edit=1

« previous php.bugs (#225065) next »