[php-src] Issue #12883: Integer Values being converted to Float with Invalid Decimal Conversion by json_encode
| From: | india081947 | Date: | Wed, 06 Dec 2023 15:27:08 +0000 |
| Subject: | [php-src] Issue #12883: Integer Values being converted to Float with Invalid Decimal Conversion by json_encode | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245994@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12883
Author: india081947
### Description
The following code:
```php
<?php
for($i=0;$i<99;$i++){
$randomint = 552;
$decval = ($i/100); //simply 0.3
$intval = ($randomint + $decval) * 100; //55230
$txt = json_encode( $intval ); //55229.99999999999?
$index = str_pad($i, 2, '0', STR_PAD_LEFT);
echo "$randomint"."$index = $txt</br>";
}
```
Resulted in this output:
```
55200 = 55200
55201 = 55201
55202 = 55202
55203 = 55203
55204 = 55204
55205 = 55204.99999999999
55206 = 55205.99999999999
55207 = 55207.00000000001
55208 = 55208.00000000001
55209 = 55209
55210 = 55210
55211 = 55211
55212 = 55212
55213 = 55213
55214 = 55214
55215 = 55215
55216 = 55216
55217 = 55216.99999999999
55218 = 55217.99999999999
55219 = 55219.00000000001
55220 = 55220.00000000001
55221 = 55221
55222 = 55222
55223 = 55223
55224 = 55224
55225 = 55225
55226 = 55226
55227 = 55227
55228 = 55228
55229 = 55229
55230 = 55229.99999999999
55231 = 55230.99999999999
55232 = 55232.00000000001
55233 = 55233.00000000001
55234 = 55234
55235 = 55235
55236 = 55236
55237 = 55237
55238 = 55238
55239 = 55239
55240 = 55240
55241 = 55241
55242 = 55241.99999999999
55243 = 55242.99999999999
55244 = 55244.00000000001
55245 = 55245.00000000001
55246 = 55246
55247 = 55247
55248 = 55248
55249 = 55249
55250 = 55250
55251 = 55251
55252 = 55252
55253 = 55253
55254 = 55254
55255 = 55254.99999999999
55256 = 55255.99999999999
55257 = 55257.00000000001
55258 = 55258.00000000001
55259 = 55259
55260 = 55260
55261 = 55261
55262 = 55262
55263 = 55263
55264 = 55264
55265 = 55265
55266 = 55266
55267 = 55266.99999999999
55268 = 55267.99999999999
55269 = 55269.00000000001
55270 = 55270.00000000001
55271 = 55271
55272 = 55272
55273 = 55273
55274 = 55274
55275 = 55275
55276 = 55276
55277 = 55277
55278 = 55278
55279 = 55279
55280 = 55279.99999999999
55281 = 55280.99999999999
55282 = 55282.00000000001
55283 = 55283.00000000001
55284 = 55284
55285 = 55285
55286 = 55286
55287 = 55287
55288 = 55288
55289 = 55289
55290 = 55290
55291 = 55291
55292 = 55291.99999999999
55293 = 55292.99999999999
55294 = 55294.00000000001
55295 = 55295.00000000001
55296 = 55296
55297 = 55297
55298 = 55298
```
But I expected this output instead:
```
55200 = 55200
55201 = 55201
55202 = 55202
55203 = 55203
55204 = 55204
55205 = 55205
55206 = 55206
55207 = 55207
55208 = 55208
55209 = 55209
55210 = 55210
55211 = 55211
55212 = 55212
55213 = 55213
55214 = 55214
55215 = 55215
55216 = 55216
55217 = 55217
55218 = 55218
55219 = 55219
55220 = 55220
55221 = 55221
55222 = 55222
55223 = 55223
55224 = 55224
55225 = 55225
55226 = 55226
55227 = 55227
55228 = 55228
55229 = 55229
55230 = 55230
55231 = 55231
55232 = 55232
55233 = 55233
55234 = 55234
55235 = 55235
55236 = 55236
55237 = 55237
55238 = 55238
55239 = 55239
55240 = 55240
55241 = 55241
55242 = 55242
55243 = 55243
55244 = 55244
55245 = 55245
55246 = 55246
55247 = 55247
55248 = 55248
55249 = 55249
55250 = 55250
55251 = 55251
55252 = 55252
55253 = 55253
55254 = 55254
55255 = 55255
55256 = 55256
55257 = 55257
55258 = 55258
55259 = 55259
55260 = 55260
55261 = 55261
55262 = 55262
55263 = 55263
55264 = 55264
55265 = 55265
55266 = 55266
55267 = 55267
55268 = 55268
55269 = 55269
55270 = 55270
55271 = 55271
55272 = 55272
55273 = 55273
55274 = 55274
55275 = 55275
55276 = 55276
55277 = 55277
55278 = 55278
55279 = 55279
55280 = 55280
55281 = 55281
55282 = 55282
55283 = 55283
55284 = 55284
55285 = 55285
55286 = 55286
55287 = 55287
55288 = 55288
55289 = 55289
55290 = 55290
55291 = 55291
55292 = 55292
55293 = 55293
55294 = 55294
55295 = 55295
55296 = 55296
55297 = 55297
55298 = 55298
```
Problem is that whenever any random number ex. 55230 in any object needs to be converted to json in
such case json_encode try to convert it in float and that too with wrong precision. This happens to
random series like 552, 632 but not all ex. 250 will run without conversion
### PHP Version
PHP 8.3.0
### Operating System
Ubuntu 20.04