3

Having a big problem with json_encode it automatically round off the digits but I need 2 decimal points on every digits.

PHP:

<?php
$numbers = [1.00,2.00];

foreach ($numbers as $i => $number) 
{
      $numbers[$i] = number_format($number, 2, '.', null);
}

echo json_encode($numbers,  JSON_NUMERIC_CHECK);
?>

OUTPUT: [1,2]

EXPECTED OUTPUT: [1.00,2.00]

how can I prevent every digits for not rounding off automatically?

PS: NOT A STRING :)

11
  • Just remove JSON_NUMERIC_CHECK parameter. It will encode your formatted numbers as strings. Commented Jun 9, 2015 at 7:58
  • 1
    An integer is just a number. It doesn't have a specific number of decimal places. You're formatting your integers to strings which can have any layout you choose, but then specifically encode the resulting strings as numbers again. You get what you're asking the code for. If you need a specific format pass the numbers as formatted strings Commented Jun 9, 2015 at 7:58
  • 1
    @sa.lva.ge No I can't because what you're doing is perverse. You want a number, your value is 1. That is the numeric value and it is identical to 1.00. If you want that value presented in a specific format you need a string. Commented Jun 9, 2015 at 8:06
  • 1
    There's absolutely no point in having numbers with extra zero decimals in JSON. You won't be able to read them in JavaScript. So they are just dead-weight there if you keep them as number. Use strings if you want to JSON to carry over number formatting. Commented Jun 9, 2015 at 8:07
  • 1
    "I need 2 decimal points" -> No you don't. Think hard on why you think you need it. Commented Jun 9, 2015 at 8:19

3 Answers 3

6

Sounds like you are looking for JSON_PRESERVE_ZERO_FRACTION, available in PHP 5.6.6. Prior versions, you'll need to either convert to string or suck it up and accept that floats and integers are equivalent when there's no fractional value.

$numbers = [1.00,2.00];

echo $res = json_encode($numbers,JSON_PRESERVE_ZERO_FRACTION);
Sign up to request clarification or add additional context in comments.

3 Comments

There is 0 difference between 1.0 and 1.00. Any code you write to deal with 1.00 will have same results with 1.0, including checking the precision. If you are concerned with output to user, use strings.
There's also 0 difference between 1.00 and 1, but tell that to the OP... :)
I think that there are minor differences when checking if 1.0 is of type float vs integer (or maybe its when checking if 1 is of type float) that could be relevant. But 1.0 and 1.00 are both strictly equivalent, and its almost certainly the case that trailing zeros are dropped by the compiler after any "double" or "float" bit is set.
2
$numbers = [1.00,2.00];

Here your numbers have already lost their "decimal values". Seriously, try var_dump on them right after this line, you won't get .00 from that either.

Floating point numbers and integers only contain the numeric value, formatting is not preserved in any way. It's not a json_encode problem, it's a fundamental truth of numeric values in computing.

If you want to preserve formatting you need to use strings all the way through.

Comments

-1

You can use your code in this way as follows:

<?php
 $numbers = [1.00,2.00];

 foreach ($numbers as $i => $number) 
 {
    $numbers[$i] = number_format($number, 2, '.', null);
 }

  echo $res = json_encode($numbers); // ["1.00","2.00"]
  echo str_replace('"', '', $res); //[1.00,2.00]
 ?>

and Your EXPECTED OUTPUT: [1.00,2.00] which is same.

3 Comments

this is so dangerous because if i had a json with string values like "name: A, "age" : 19.... it will remove also the "" on the string
Ok. I replied according to your question and expected result.
If what the OP wants is nonsense then there's no need to bend over backwards to fulfil their wish.

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.