Cara menggunakan php json_encode float precision

This library provides a PHP extension that adds support for correctly-rounded, arbitrary-precision decimal floating point arithmetic. Applications that rely on accurate numbers (ie. money, measurements, or mathematics) can use pecl install decimal 0 instead of pecl install decimal 1 or pecl install decimal 2 to represent numerical values.

The implementation uses the same arbitrary precision library as Python’s decimal, called mpdecimal.

The decimal extension offers several advantages over the pecl install decimal 1 data type:

  • All rational numbers can be represented accurately. In contrast, numbers like pecl install decimal 4 do not have exact representations in binary floating point. You can read more about this in the floating point guide or in PHP’s documentation.
  • While a binary floating point value like pecl install decimal 4 comes close, the small difference prevents reliable equality testing, and inaccuracies may accumulate over time.
  • Arbitrary precision allows for numbers that are not bound by the same upper and lower limits as pecl install decimal 1 - numbers can be as big or small as required.
  • PHP does a good job of hiding the inaccuracies of binary floating point representation with the INI setting. By default, pecl install decimal 7 will have a string value of pecl install decimal 8 even though the internal C pecl install decimal 9 can not represent the result accurately. For example:

var_dump(0.1 + 0.2); // float(0.3) var_dump(0.1 + 0.2 - 0.3); // float(5.5511151231258E-17)

PHP already has arbitrary precision math functions…

The current goto answer for arbitrary precision math in PHP is bcmath. However, the pecl install decimal 0 class offers multiple advantages over bcmath:

  • Decimal values are objects, so you can typehint pecl install decimal 0 instead of pecl install decimal 2.
  • Arithmetic and comparison operators are supported.
  • Precision is defined as the number of significant figures, and scale is the number of digits behind the decimal point. This means that a number like phpbrew ext install decimal phpbrew ext enable decimal 3 would require a scale of phpbrew ext install decimal phpbrew ext enable decimal 4 but a precision of phpbrew ext install decimal phpbrew ext enable decimal 5. This library uses precision; bcmath uses scale.
  • Scientific notation is supported, so you can use strings like phpbrew ext install decimal phpbrew ext enable decimal 6 to construct a pecl install decimal 0. At the time of this writing, you can not do this with bcmath.
  • Calculations are significantly faster. See for some benchmarks.

Installation

Dependencies

  • PHP 7
  • libmpdec 2.4+

Composer

Composer can not be used to install the extension. The phpbrew ext install decimal phpbrew ext enable decimal 8 package can be used to specify the extension as a dependency and provides stubs for IDE integration. If you are using Composer and would like to add this extension as a dependency, require phpbrew ext install decimal phpbrew ext enable decimal 8.

Install

The easiest way to install the extension is to use PECL:

pecl install decimal

If you are using phpbrew:

phpbrew ext install decimal phpbrew ext enable decimal

Enable

Remember to enable to extension in your .ini file.

extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows

Verify

You can confirm that the extension is installed with extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 0.

Basic Usage

The pecl install decimal 0 class is under the pecl install decimal 0 namespace.

pecl install decimal 0 objects can be constructed using a pecl install decimal 0, pecl install decimal 2, or extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6 value, and an optional precision which defaults to 28.

Special pecl install decimal 1 values are also supported (extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 8, extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9 and use Decimal\Decimal; $a = new Decimal("1", 2); $b = new Decimal("7", 8); print_r($a / $b); 0), but pecl install decimal 1 is otherwise not a valid argument in order to avoid accidentially using a pecl install decimal 1. If you absolutely must use a pecl install decimal 1 to construct a decimal you can cast it to a pecl install decimal 2 first, but doing so if affected by the INI setting.

Projects using this extension should avoid pecl install decimal 1 entirely, wherever possible. An example workflow is to store values as use Decimal\Decimal; $a = new Decimal("1", 2); $b = new Decimal("7", 8); print_r($a / $b); 6 in the database, query them as pecl install decimal 2, parse to pecl install decimal 0, perform calculations, and finally prepare for the database using .

JSON conversions will automatically convert the decimal to pecl install decimal 2 using all signficant figures.

A warning will be raised if value was not parsed completely. For example, Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 0 to a precision of Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 1 will result in Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 2 with a warning. Similarly, Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 3 with a precision of Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 1 would result in Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 5 with a warning because data has been lost.

pecl install decimal 0 is final and immutable. Arithmetic operations always return a new pecl install decimal 0 using the maximum precision of the object and the operands. The result is therefore accurate up to Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 ) 8 significant figures, subject to rounding of the last digit.

For example:

use Decimal\Decimal; $a = new Decimal("1", 2); $b = new Decimal("7", 8); print_r($a / $b);

Decimal\Decimal Object ( [value] => 0.14285714 [precision] => 8 )

Scalar operands inherit the precision of the pecl install decimal 0 operand, which avoids the need to construct a new object for the operation. If a scalar operand must be parsed with a higher precision, you should construct a new pecl install decimal 0 with an explicit precision. The result of a decimal operation is always a pecl install decimal 0.

For example:

use Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32);

Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 )

Sandbox

This is a limited environment where you can experiment with pecl install decimal 0.

<?php use Decimal\Decimal; $a = new Decimal("0.1"); $b = new Decimal("7.0"); echo $a / $b;

Run Example Reset

Performance

The benchmark performs calculations on pecl install decimal 2 and extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6 values, then converts the result to a string at the very end. While this does not represent cases where a single operation is performed and exported, the goal is to simulate a realistic workflow where a number is created, used in a few calculations, and exported at the end.

It is difficult to determine what to use for the scale of bcmath, because it specifies the number of places behind the decimal point, rather than the precision, which is the total number of significant places. This benchmark is therefore arbitrary in itself and serves only to provide a rough idea of what to expect.

The code for this basic benchmark can be found here.

Results are the total runtime to produce a result across many iterations, in seconds. Lower is better.

Results

 TypeAddSubtractMultiplyDivideuse Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32); 5pecl install decimal 2use Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32); 7use Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32); 8use Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32); 9Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 0Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 1pecl install decimal 2Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 3Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 4Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 5Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 6use Decimal\Decimal; $op1 = new Decimal("0.1", 4); $op2 = "0.123456789"; print_r($op1 + $op2); use Decimal\Decimal; /** * @param int $n The factorial to calculate, ie. $n! * @param int $p The precision to calculate the factorial to. * * @return Decimal */ function factorial(int $n, int $p = Decimal::DEFAULT_PRECISION): Decimal { return $n < 2 ? new Decimal($n, $p) : $n * factorial($n - 1, $p); } echo factorial(10000, 32); 5extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 9$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 0$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 1$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 2Warning: Loss of data on string conversion in ... on line 1 Decimal\Decimal Object ( [value] => 0.2235 [precision] => 4 ) 1extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 5$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 6$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 7$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 8

Attributes

Precision

Precision defines the number of significant figures that a decimal is accurate to. For example, a number like $a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64 9 is very small but only has 3 significant figures. PHP decimal uses a default precision of use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 0 and does not take the precision setting in the .ini into account (which is for converting pecl install decimal 1 to string). Increasing the precision will require more memory and might impact runtime significantly for operations like use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 2 and use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 3 when using a very high precision.

Decimal operations, casting and construction will always preserve precision to avoid data loss:

  • You can’t reduce the precision of a decimal.
  • You can’t change the value of a decimal after it has been calculated.
  • Constructing a decimal using a decimal will preserve the given decimal’s precision.

For example:

$a = new Decimal("0.1", 50); $b = new Decimal($a); // Precision is 50 $c = new Decimal($b, 6); // Precision is 50 $d = new Decimal($c, 64); // Precision is 64

Arithmetic operations will result in a new decimal using the maximum precision of all operands. The developer’s only responsibility is to define the precision (as a minimum) when constructing a decimal. For example, if you have a use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 4 column in your database (precision is 20, scale is 6), you would create a decimal instance using 20 for the precision and be assured that all calculations will use and result in a precision of at least 20. When the value is to be written back to the database, you would use use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 5 to produce a string rounded accurately to 6 decimal places to match the scale of the SQL data type.

There are three precision constants:

  • use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 6
  • use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 7
  • use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5 8

->precision(): int

Returns: 

int, the precision of this decimal.

Special Numbers

There are 3 special numbers: extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9, use Decimal\Decimal; $a = new Decimal("1", 2); $b = new Decimal("7", 8); print_r($a / $b); 0 and extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 8. These correspond to the same pecl install decimal 1 value constants in PHP. All comparison and arithmetic using these values match the behaviour of PHP pecl install decimal 1 values wherever possible, and any case that does not do so is considered a bug.

->isNaN(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is not a defined number.

->isInf(): bool

Returns: 

bool, pecl install decimal 04 if this decimal represents infinity, pecl install decimal 06 otherwise.

Integers

->isInteger(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is an integer, ie. does not have significant figures behind the decimal point, otherwise pecl install decimal 06.

->isZero(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is either positive or negative zero.

Sign

->abs(): Decimal

Returns: 

Decimal, the absolute (positive) value of this decimal.

->negate(): Decimal

Returns: 

Decimal, the same value as this decimal, but the sign inverted.

->signum(): int

Returns: 

int, pecl install decimal 10 if zero, pecl install decimal 11 if negative, or pecl install decimal 12 if positive.

->isPositive(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is positive, pecl install decimal 06 otherwise.

->isNegative(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is negative, pecl install decimal 06 otherwise.

Parity

->parity(): int

Returns: 

int, pecl install decimal 10 if the integer value of this decimal is even, pecl install decimal 12 if odd. Special numbers like extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 8 and extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9 will return pecl install decimal 12.

->isEven(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is an integer and even, pecl install decimal 06 otherwise.

->isOdd(): bool

Returns: 

bool, pecl install decimal 04 if this decimal is an integer and odd, pecl install decimal 06 otherwise.

Rounding

The default rounding mode defined as pecl install decimal 26 is , which is also the default used by IEEE 754, , Java, and . However, uses , and both and PHP use .

The reason for this default is to prevent biasing the average upwards or downwards.

This stack exchange answer provides some great examples for further reading.

Rounding Modes

The default rounding mode can not be changed because it affects how values are reduced to a precision. With a fixed internal rounding mode, an input value will always result in the same decimal value for a given precision, regardless of the environment. However, some methods allow you to provide a rounding mode, which can be any of the following constants:

  • pecl install decimal 27 (away from from)
  • pecl install decimal 28 (towards zero)
  • pecl install decimal 29 (towards positive infinity)
  • pecl install decimal 30 (towards negative infinity)
  • pecl install decimal 31 (halfway ties away from zero)
  • pecl install decimal 32 (halfway ties towards zero)
  • pecl install decimal 33 (halfway ties to nearest even number)
  • pecl install decimal 34 (halfway ties to nearest odd number)
  • pecl install decimal 35 (no rounding)

You can also use the corresponding PHP constants.

Rounding Methods

->floor(): Decimal

Returns: 

Decimal, the closest integer towards negative infinity.

->ceil(): Decimal

Returns: 

Decimal, the closest integer towards positive infinity.

->truncate(): Decimal

Returns: 

Decimal, the result of discarding all digits behind the decimal point.

->round(int $places = 0int $mode = Decimal::ROUND_HALF_EVEN): Decimal

Returns: 

Decimal, the value of this decimal with the same precision, rounded according to the specified number of decimal places and rounding mode.

Throws:

  • pecl install decimal 36 if the rounding mode is not supported.

->trim(): Decimal

Returns: 

Decimal, a copy of this decimal without trailing zeroes.

Since:

1.1.0

->toFixed(int $places = 0bool $commas = falseint $mode = Decimal::ROUND_HALF_EVEN): string

Returns: 

string, the value of this decimal formatted to a fixed number of decimal places, with thousands comma-separated, using a given rounding mode.

Comparing

Decimal objects are equal if their numeric values are equal, as well as their precision. The only value that breaks this rule is extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 8, which is not equal to anything else including itself. Precision is used as the tie-break in cases where the values are equal.

Decimal objects can be compared to any other type to determine equality or relative ordering. Non-decimal values will be converted to decimal first (using the maximum precision). In cases where the type is not supported or comparison is not defined (eg. a decimal compared to pecl install decimal 38), the decimal is considered greater and an exception will not be thrown.

While decimal objects can not be constructed from a non-special pecl install decimal 1, they can be compared to pecl install decimal 1. This is done by implicitly converting the value to a string using the equivalent of a string cast. This conversion is affected by the .ini “precision” setting because an implicit cast should have the same behaviour as an explicit cast.

Decimal objects follow the standard PHP object conventions:

  • Always pecl install decimal 41.
  • Always greater than pecl install decimal 42.
  • Identical (pecl install decimal 43) if they are the same object, even if equal.

There are two methods that you can use to compare:

->equals(mixed $other): bool

This method is equivalent to the pecl install decimal 44 operator.

Returns: 

bool, pecl install decimal 04 if this decimal is considered equal to the given value.

->compareTo(mixed $other): int

This method is equivalent to the pecl install decimal 46 operator.

Returns: 

int, pecl install decimal 10 if this decimal is considered equal to pecl install decimal 48,
pecl install decimal 11 if this decimal should be placed before pecl install decimal 48,
pecl install decimal 12 if this decimal should be placed after pecl install decimal 48.

Operators

MethodOperatorsDescriptionpecl install decimal 53pecl install decimal 46, pecl install decimal 55, pecl install decimal 56, pecl install decimal 57, pecl install decimal 58Relative ordering, sorting.pecl install decimal 59pecl install decimal 44Equality, equal precision. pecl install decimal 43Identity, same exact object, even if equal.

Converting

Decimal objects can be converted to pecl install decimal 2, extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6, and pecl install decimal 1.

->toInt(): int

This method is equivalent to a cast to int.

Returns: 

int, the integer value of this decimal.

Throws:

  • pecl install decimal 65 if the value is greater than pecl install decimal 66.

->toFloat(): float

This method is equivalent to a cast to float, and is not affected by the 'precision' INI setting.

Returns: 

float, the native PHP floating point value of this decimal.

Throws:

  • pecl install decimal 65 if the value is greater than pecl install decimal 68.
  • pecl install decimal 69 if the value is smaller than pecl install decimal 70.

->toString(): string

This method is equivalent to a cast to string.

Returns: 

string, the value of this decimal represented exactly, in either fixed or scientific form, depending on the value.

Casting

You can also cast a decimal to pecl install decimal 2, pecl install decimal 1, extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 6 and pecl install decimal 74.

use Decimal\Decimal; (bool) new Decimal(); // true, by convention (bool) new Decimal(1); // true (int) new Decimal("1.5"); // 1 (float) new Decimal("1.5"); // 1.5 (string) new Decimal("1.5"); // 1.5

Important: pecl install decimal 75 or pecl install decimal 76 should not be used to produce a canonical representation of a decimal, because there is more than one way to represent the same value, and precision is not represented by the value itself. However, it is guaranteed that the string representation of a decimal can be used to construct a new decimal with the exact same value, assuming equal precision. If you want to store a decimal with its precision, you should use pecl install decimal 77 and pecl install decimal 78.

Arithmetic

Methods

->add(Decimal|string|int $value): Decimal

This method is equivalent to the pecl install decimal 79 operator.

Returns: 

Decimal, the result of adding this decimal to the given value.

Throws:

  • pecl install decimal 80 if the value is not a decimal, string or integer.

->sub(Decimal|string|int $value): Decimal

This method is equivalent to the pecl install decimal 81 operator.

Returns: 

Decimal, the result of subtracting a given value from this decimal.

Throws:

  • pecl install decimal 80 if the value is not a decimal, string or integer.

->mul(Decimal|string|int $value): Decimal

This method is equivalent to the pecl install decimal 83 operator.

Returns: 

Decimal, the result of multiplying this decimal by the given value.

Throws:

  • pecl install decimal 80 if the given value is not a decimal, string or integer.

->div(Decimal|string|int $value): Decimal

This method is equivalent to the pecl install decimal 85 operator.

Returns: 

Decimal, the result of dividing this decimal by the given value.

Throws:

  • pecl install decimal 80 if the value is not a decimal, string or integer.
  • pecl install decimal 87 if dividing by zero.
  • pecl install decimal 88 if division is undefined, eg. extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9 / -extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9

->mod(Decimal|string|int $value): Decimal

This method is equivalent to the pecl install decimal 91 operator.

Returns: 

Decimal, the remainder after dividing the integer value of this decimal by the integer value of the given value.

Throws:

  • pecl install decimal 80 if the value is not a decimal, string or integer.
  • pecl install decimal 87 if the integer value of $value is zero.
  • pecl install decimal 88 if the operation is undefined, eg. extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9 % -extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9

->rem(Decimal|string|int $value): Decimal

Returns: 

Decimal, the remainder after dividing this decimal by a given value.

Throws:

  • pecl install decimal 80 if the value is not a decimal, string or integer.
  • pecl install decimal 87 if the integer value of $value is zero.
  • pecl install decimal 88 if the operation is undefined, eg. extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9, -extension=decimal.so # Unix, OS X extension=php_decimal.dll # Windows 9

->pow(Decimal|string|int $exponent): Decimal

This method is equivalent to the phpbrew ext install decimal phpbrew ext enable decimal 02 operator.

Returns: 

Decimal, the result of raising this decimal to a given power.

Throws:

  • pecl install decimal 80 if the exponent is not a decimal, string or integer.

->shift(int $places): Decimal

Returns: 

Decimal, A copy of this decimal with its decimal place shifted.

->ln(): Decimal

This method is equivalent in function to PHP's phpbrew ext install decimal phpbrew ext enable decimal 04.

Returns: 

Decimal, the natural logarithm of this decimal (log base e), with the same precision as this decimal.

->exp(): Decimal

Returns: 

Decimal, the exponent of this decimal, ie. e to the power of this, with the same precision as this decimal.

->log10(): Decimal

Returns: 

Decimal, the base-10 logarithm of this decimal, with the same precision as this decimal.

->sqrt(): Decimal

Returns: 

Decimal, the square root of this decimal, with the same precision as this decimal.

Decimal::sum(array|Traversable $valuesint $precision = 28): Decimal

The precision of the result will be the max of all precisions that were encountered during the calculation. The given precision should therefore be considered the minimum precision of the result. This method is equivalent to adding each value individually, then dividing by the number of values.

Postingan terbaru

LIHAT SEMUA