encoding/decoding json is giving me random decimals for all my numbers
The json I am reading has completely different numbers then the one that outputs to me. How can I fix this? I already tried using round() to round each number in a foreach loop. My php version is 7.2.13
external.json
{
"fruits": {
"banana": 84.4234563919,
"apple": 0.0715972684,
"orange": 4.808779748
}
}
output.json
{
"fruits": {
"banana": 84.4234563919000038367812521755695343017578125,
"apple": 0.0715972683999999970350103239979944191873073577880859375,
"orange": 4.80877974800000007604694474139250814914703369140625
}
}
test.php
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
$new_json = json_encode($json1, JSON_PRETTY_PRINT);
file_put_contents("test.json", $new_json);
?>
test2.php (trying to use round function) but still the same result
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2);
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
php json
add a comment |
The json I am reading has completely different numbers then the one that outputs to me. How can I fix this? I already tried using round() to round each number in a foreach loop. My php version is 7.2.13
external.json
{
"fruits": {
"banana": 84.4234563919,
"apple": 0.0715972684,
"orange": 4.808779748
}
}
output.json
{
"fruits": {
"banana": 84.4234563919000038367812521755695343017578125,
"apple": 0.0715972683999999970350103239979944191873073577880859375,
"orange": 4.80877974800000007604694474139250814914703369140625
}
}
test.php
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
$new_json = json_encode($json1, JSON_PRETTY_PRINT);
file_put_contents("test.json", $new_json);
?>
test2.php (trying to use round function) but still the same result
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2);
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
php json
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29
add a comment |
The json I am reading has completely different numbers then the one that outputs to me. How can I fix this? I already tried using round() to round each number in a foreach loop. My php version is 7.2.13
external.json
{
"fruits": {
"banana": 84.4234563919,
"apple": 0.0715972684,
"orange": 4.808779748
}
}
output.json
{
"fruits": {
"banana": 84.4234563919000038367812521755695343017578125,
"apple": 0.0715972683999999970350103239979944191873073577880859375,
"orange": 4.80877974800000007604694474139250814914703369140625
}
}
test.php
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
$new_json = json_encode($json1, JSON_PRETTY_PRINT);
file_put_contents("test.json", $new_json);
?>
test2.php (trying to use round function) but still the same result
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2);
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
php json
The json I am reading has completely different numbers then the one that outputs to me. How can I fix this? I already tried using round() to round each number in a foreach loop. My php version is 7.2.13
external.json
{
"fruits": {
"banana": 84.4234563919,
"apple": 0.0715972684,
"orange": 4.808779748
}
}
output.json
{
"fruits": {
"banana": 84.4234563919000038367812521755695343017578125,
"apple": 0.0715972683999999970350103239979944191873073577880859375,
"orange": 4.80877974800000007604694474139250814914703369140625
}
}
test.php
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
$new_json = json_encode($json1, JSON_PRETTY_PRINT);
file_put_contents("test.json", $new_json);
?>
test2.php (trying to use round function) but still the same result
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2);
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
php json
php json
edited Dec 25 '18 at 8:35
Paul
asked Dec 25 '18 at 8:03
PaulPaul
205
205
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29
add a comment |
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29
add a comment |
1 Answer
1
active
oldest
votes
in php 7.0 double numbers has this problem.
see this bug
my answer is change double numbers to string and after decode change it to number (you can add zero after decode)
or you can change your php.ini
serialize_precision = -1
see precision integer
You can add a empty string to each double value of your array,
Example for test2.php:
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2).'';
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
Can you show me how using mytest2.phpcode as an example?
– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from theexternal.json?
– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53920472%2fencoding-decoding-json-is-giving-me-random-decimals-for-all-my-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
in php 7.0 double numbers has this problem.
see this bug
my answer is change double numbers to string and after decode change it to number (you can add zero after decode)
or you can change your php.ini
serialize_precision = -1
see precision integer
You can add a empty string to each double value of your array,
Example for test2.php:
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2).'';
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
Can you show me how using mytest2.phpcode as an example?
– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from theexternal.json?
– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
add a comment |
in php 7.0 double numbers has this problem.
see this bug
my answer is change double numbers to string and after decode change it to number (you can add zero after decode)
or you can change your php.ini
serialize_precision = -1
see precision integer
You can add a empty string to each double value of your array,
Example for test2.php:
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2).'';
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
Can you show me how using mytest2.phpcode as an example?
– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from theexternal.json?
– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
add a comment |
in php 7.0 double numbers has this problem.
see this bug
my answer is change double numbers to string and after decode change it to number (you can add zero after decode)
or you can change your php.ini
serialize_precision = -1
see precision integer
You can add a empty string to each double value of your array,
Example for test2.php:
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2).'';
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
in php 7.0 double numbers has this problem.
see this bug
my answer is change double numbers to string and after decode change it to number (you can add zero after decode)
or you can change your php.ini
serialize_precision = -1
see precision integer
You can add a empty string to each double value of your array,
Example for test2.php:
<?php
$contents1 = file_get_contents("external.json");
$json1 = json_decode($contents1, TRUE);
foreach ($json1['fruits'] as $key=>$value) {
$new_json['fruits'][$key] = round($value, 2).'';
}
$encoded_json = json_encode($new_json, JSON_PRETTY_PRINT);
file_put_contents("test.json", $encoded_json );
?>
edited Dec 25 '18 at 8:35
answered Dec 25 '18 at 8:11
ttrasnttrasn
324513
324513
Can you show me how using mytest2.phpcode as an example?
– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from theexternal.json?
– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
add a comment |
Can you show me how using mytest2.phpcode as an example?
– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from theexternal.json?
– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
Can you show me how using my
test2.php code as an example?– Paul
Dec 25 '18 at 8:25
Can you show me how using my
test2.php code as an example?– Paul
Dec 25 '18 at 8:25
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
@Paul I will edit my answer
– ttrasn
Dec 25 '18 at 8:32
What if I want the original value? I don't want to round to anything, but just the original value from the
external.json?– Paul
Dec 25 '18 at 8:49
What if I want the original value? I don't want to round to anything, but just the original value from the
external.json?– Paul
Dec 25 '18 at 8:49
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
when the number is changed you can not find the real number but you can found near number to it with round()
– ttrasn
Dec 25 '18 at 9:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53920472%2fencoding-decoding-json-is-giving-me-random-decimals-for-all-my-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I cannot reproduce your issue with straight PHP as you have above. Usually this issue occurs in Javascript.
– Emmanuel
Dec 25 '18 at 8:13
@Emmanuel I have no javascript in my code
– Paul
Dec 25 '18 at 8:29