2

I need execute json_encode() and convert my original number from:

50610101800060384093800100001010000000056199999999

to

"50610101800060384093800100001010000000056199999999" 

But it return

5.061010180006E+49

I tried this:

ini_set('precision', 30); //With 1, 30, 50, 100, 1000
ini_set('serialize_precision', -1);
'content' => json_encode($params, JSON_NUMERIC_CHECK)

but doesn't work. Can you help me?

3
  • You should convert your number to string before using json_encode() and do not use JSON_NUMERIC_CHECK flag. Commented Oct 10, 2018 at 8:27
  • 3
    Did you at least read what is the precision? How do you want to store such large numbers as integers, ever heard of the PHP_INT_MAX ? Commented Oct 10, 2018 at 10:19
  • It's being turned into scientific notation before you get to the json_encode. Your code you've displayed is missing the crucial part of where you are assigning the variable the large number, or where it's coming from, as that's where you need to fix it. Commented Oct 10, 2018 at 17:57

2 Answers 2

3

50610101800060384093800100001010000000056199999999 exceeds the value of the maximum integer in PHP and so it is promoted to a float and expressed in scientific notation. The float result may be problematic for various reasons as the Manual explains in warning about floating point precision.

If you wish to express the value as if it were an integer you must encapsulate it in a string. That string you may add zero to it but when you do so the result in scientific notation will refer to a float, as follows:

<?php

$s = "50610101800060384093800100001010000000056199999999";

echo $s,"\n";

$x = $s + 0;

echo $x, "\n",is_float($x); 

See here.

For more info in re PHP and floats, see here.

On the other hand, if there were an array of numbers whose digits corresponded to the numerical display in the OP's post, you could write code as follows:

<?php
    $a = [5,0,6,1,0,1,0,1,8,0,0,0,6,0,3,8,4,0,9,3,8,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,5,6,1,9,9,9,9,9,9,9,9];
    foreach($a as $e) {
       $e = (string) $e;
    }
    $foo = join($a);
    var_dump($foo);
    $foo = bcadd($foo, 1);
    var_dump($foo);

See live code.

The reason this example works is because each array value is converted to a numerical string and then the individual elements are joined to form one very long numerical string. BC Math is an extension in PHP which supports arbitrary precision. In this case, the bcadd() function adds one to the numerical string which results in the display of an incremented numerical string value.

Sign up to request clarification or add additional context in comments.

Comments

2

Try This [https://3v4l.org/biNJG][1]

If you want this output "50610101800060384093800100001010000000056199999999" you may want to pass this Value as string after encoding the value to JSON using json_encode An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:

  1. An integer must have at least one digit

  2. An integer must not have a decimal point

  3. An integer can be either positive or negative

  4. Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.