0

I have a large number [987654321999999] that i wanna pass it to jquery but it return large number with scientific notation [9.87654322E+14]. How can i send the large number to jquery without scientific notation?


PHP function

public function actionCalPrice()
{
$totalPrice = 987654321999999;
echo CJSON::encode($totalPrice);
}

In JQuery

$.ajax({
  type: "POST",
  url: "<?php echo Yii::app()->createUrl('items/calPrice');?>",
  data:{ajax:ajax},
  dataType: "json",
  success: function(data) {
    alert(data);
    }
});
SADi
  • 295
  • 2
  • 12

3 Answers3

1

When working with large integers it is best to use a string as opposed to integer.

public function actionCalPrice()
{
   $totalPrice = "987654321999999";
   echo CJSON::encode($totalPrice);
}

or in case you can't wrap quotes around the number lookup sprintf for additional formatting.

public function actionCalPrice()
{
   $totalPrice = 987654321999999;
   echo CJSON::encode(rtrim(sprintf('%.20F', $totalPrice),'0'));
}

However your Javascript will perform differently on 32-bit operating systems/applications.

What is your desired Javascript output?

Additionally if your $totalPrice is actually a variable as opposed to a constant, such as a calculation of a price from other variables, I suggest looking into bcmath for PHP. http://php.net/manual/en/book.bc.php

Will B.
  • 17,883
  • 4
  • 67
  • 69
0

987654321999999 is even bigger number than PHP can handle. PHP_INT_MAX is 2147483647 so it tries to find the closest number possible. However if you have this number stored somewhere you can convert it to a string using for example number_format which magically (I really don't know how) keeps the original number:

$totalPrice = 987654321999999;
$str = number_format($totalPrice, 0, ".", "");
echo $str;

If you then parse this using the JSON.parse function you will get the original number (because JS has larger numbers).

Edit: Yes, of course, if you are running 64-bit OS (I'm running it too and I just realized it :D), the size for INT is bigger. But PHP can't somehow print these numbers.

9999999999999999 behaves really strange. 9999999999999999 === 10000000000000000 evalutes to true.

Razem
  • 1,421
  • 11
  • 14
  • `2147483647` is the maximum 32-bit integer size, `9223372036854775807` is the maximum 64-bit integer size – Will B. Jul 22 '14 at 11:57
  • Thanks, but your answer doesn't work for **9999999999999999** number, it rounds to **10000000000000000**. – SADi Jul 22 '14 at 11:58
  • This is due to floating points in PHP, do not use integers/floating points with large numbers. – Will B. Jul 22 '14 at 12:20
  • @fyrye Yep, that's it. So it's really better to use the bc library you mentioned. – Razem Jul 22 '14 at 12:21
  • You'll get the same/similar result in Javascript (and other languages), not enough memory to handle the values. another example would be `var_dump((0.1+0.2 === 0.3)); //false` in either language – Will B. Jul 22 '14 at 12:23
  • Yes I know, but in JS it starts to behave buggy with much larger numbers than in PHP. But for a work with large numbers in JS I also usually use [big.js](https://github.com/MikeMcl/big.js/). – Razem Jul 22 '14 at 12:26
  • @Radek In regards to `But PHP can't somehow print these numbers` PHP is locale aware, when dealing with large numbers and will output based on the precision setting for formatting when converting it to a string. Use `printf('%1$s', $num)` vs `printf('%1$F', $num)` use `ini_set('precision', 64);` prior and see the difference with string. – Will B. Jul 22 '14 at 12:35
  • @Radek Both Javascript and PHP use the same IEEE standard for floating points, the resulting "buggy" behavior would more than likely be a 32-bit application running the Javascript and a 64-bit server running PHP making the numeric operations in Javascript significantly smaller due to the lower memory. – Will B. Jul 22 '14 at 12:52
0

Thanks all, I solved my problem with GNU Multiple Precision. Useful link

public function actionCalPrice()
{
  $criteria = new CDbCriteria;
  $criteria->select='id,type_id,col1,col2,col3';
  $item = Items::model()->findByPk($opItem,$criteria);
  $totalPrice = gmp_add($totalPrice, $item->col2);
  echo CJSON::encode(gmp_strval($totalPrice));
}
Community
  • 1
  • 1
SADi
  • 295
  • 2
  • 12
  • Be careful when using GMP math vs BCMath. GMP handles integer values and order of operations weirdly at times. Also please provide your resulting work with GMP as opposed to just the link to the resource. Reference: http://stackoverflow.com/questions/6961685/arbitrary-precision-math-in-php – Will B. Jul 22 '14 at 12:43
  • Thanks for your note, but it just is a simple calculation on integer values. – SADi Jul 22 '14 at 13:10