decimal
1.5.0
객체 지향 BC Math 확장 래퍼/심입니다.
decimal 가능한 경우 BC Math를 사용하여 기본 임의 정밀도 연산의 무상태유창한 객체 지향 구현을 제공합니다.
부동 소수점 정밀도에 대한 자세한 내용은 여기에서 확인할 수 있습니다.
예:
use PrestaShop decimal decimal Number ;
use PrestaShop decimal Operation Rounding ;
echo ( new decimal Number ( ' 0.1 ' ))
-> plus ( new decimal Number ( ' 0.7 ' ))
-> times ( new decimal Number ( ' 10 ' ))
-> round ( 0 , Rounding:: ROUND_FLOOR )
// echoes '8'
작곡가를 통해
$ composer require prestashop/ decimal
빠른 링크:
새로운 decimal 를 생성합니다.
public __construct ( string $ number [, int $ exponent = null ] ): decimal Number
decimal decimal 를 인스턴스화하는 방법에는 두 가지가 있습니다.
// create a number from string
$ number = new PrestaShop decimal decimal Number ( ' 123.456 ' );
echo $ number ; // echoes '123.456'
// exponent notation
$ number = new PrestaShop decimal decimal Number ( ' 123456 ' , - 3 );
echo $ number ; // echoes '123.456'
현재 숫자에 다른 숫자를 더한 계산 결과를 반환합니다.
public decimal Number:: plus ( decimal Number $ addend ): decimal Number
예:
$ a = new PrestaShop decimal decimal Number ( ' 123.456 ' );
$ b = new PrestaShop decimal decimal Number ( ' 654.321 ' );
echo $ a -> plus ( $ b ); // echoes '777.777'
현재 숫자에서 다른 숫자를 뺀 계산 결과를 반환합니다.
public decimal Number:: minus ( decimal Number $ subtrahend ): decimal Number
예:
$ a = new PrestaShop decimal decimal Number ( ' 777.777 ' );
$ b = new PrestaShop decimal decimal Number ( ' 654.321 ' );
echo $ a -> minus ( $ b ); // echoes '123.456'
현재 숫자에 다른 숫자를 곱하여 계산된 결과를 반환합니다.
public decimal Number:: times ( decimal Number $ factor ): decimal Number
예:
$ a = new PrestaShop decimal decimal Number ( ' 777.777 ' );
$ b = new PrestaShop decimal decimal Number ( ' 654.321 ' );
echo $ a -> times ( $ b ); // echoes '508915.824417'
특정 decimal 자릿수(기본값은 6)까지 현재 숫자를 다른 숫자로 나눈 계산 결과를 반환합니다.
public decimal Number:: dividedBy ( decimal Number $ divisor [, int $ precision = Operation Division:: DEFAULT_PRECISION ] )
예:
$ a = new PrestaShop decimal decimal Number ( ' 777.777 ' );
$ b = new PrestaShop decimal decimal Number ( ' 654.321 ' );
echo $ a -> dividedBy ( $ b , 0 ); // echoes '1'
echo $ a -> dividedBy ( $ b , 5 ); // echoes '1.18867'
echo $ a -> dividedBy ( $ b , 10 ); // echoes '1.1886780341'
echo $ a -> dividedBy ( $ b , 15 ); // echoes '1.188678034175886'
비교 주장의 결과를 반환합니다.
$ a = new PrestaShop decimal decimal Number ( ' 777.777 ' );
$ b = new PrestaShop decimal decimal Number ( ' 654.321 ' );
$ a -> equals ( $ b ); // returns false
$ a -> isLowerThan ( $ b ); // returns false
$ a -> isLowerOrEqualThan ( $ b ); // returns false
$ a -> isGreaterThan ( $ b ); // returns true
$ a -> isGreaterOrEqualThan ( $ b ); // returns true
// shortcut methods
$ a -> equalsZero (); // returns false
$ a -> isLowerThanZero (); // returns false
$ a -> isLowerOrEqualThanZero (); // returns false
$ a -> isGreaterThanZero (); // returns true
$ a -> isGreaterOrEqualThanZero (); // returns true
숫자를 정확한 decimal 자릿수와 함께 선택적으로 반올림된 문자열로 반환합니다.
public decimal Number:: toPrecision ( int $ precision [, string $ roundingMode = Rounding:: ROUND_TRUNCATE ] ): string
예:
$ a = new PrestaShop decimal decimal Number ( ' 123.456 ' );
$ a = new PrestaShop decimal decimal Number ( ' -123.456 ' );
// truncate / pad
$ a -> toPrecision ( 0 ); // '123'
$ a -> toPrecision ( 1 ); // '123.4'
$ a -> toPrecision ( 2 ); // '123.45'
$ a -> toPrecision ( 3 ); // '123.456'
$ a -> toPrecision ( 4 ); // '123.4560'
$ b -> toPrecision ( 0 ); // '-123'
$ b -> toPrecision ( 1 ); // '-123.4'
$ b -> toPrecision ( 2 ); // '-123.45'
$ b -> toPrecision ( 3 ); // '-123.456'
$ b -> toPrecision ( 4 ); // '-123.4560'
// ceil (round up)
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '124'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '123.5'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '123.46'
$ b -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '-122'
$ b -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '-123.3'
$ b -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_CEIL ); // '-123.44'
// floor (round down)
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '123.4'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '123.45'
$ b -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '-124'
$ b -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '-123.5'
$ b -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_FLOOR ); // '-123.46'
// half-up (symmetric half-up)
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '123.5'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '123.46'
$ b -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '-123'
$ b -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '-123.5'
$ b -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_UP ); // '-123.46'
// half-down (symmetric half-down)
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '123.4'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '123.46'
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '-123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '-123.4'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_DOWN ); // '-123.46'
// half-even
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '123.4'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '123.46'
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '-123'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '-123.4'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '-123.46'
$ a = new PrestaShop decimal decimal Number ( ' 1.1525354556575859505 ' );
$ a -> toPrecision ( 0 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1'
$ a -> toPrecision ( 1 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.2'
$ a -> toPrecision ( 2 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.15'
$ a -> toPrecision ( 3 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.152'
$ a -> toPrecision ( 4 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.1525'
$ a -> toPrecision ( 5 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.15255'
$ a -> toPrecision ( 6 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.152535'
$ a -> toPrecision ( 7 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.1525354'
$ a -> toPrecision ( 8 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.15253546'
$ a -> toPrecision ( 9 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.152535456'
$ a -> toPrecision ( 10 , PrestaShop decimal Operation Rounding:: ROUND_HALF_EVEN ); // '1.1525354556'
반올림은 toPrecision
처럼 동작하지만 특정 decimal 자릿수 "최대"를 제공합니다(후행 0을 추가하지 않음).
public decimal Number:: round ( int $ max decimal s [, string $ roundingMode = Rounding:: ROUND_TRUNCATE ] ): string
예:
$ a = new PrestaShop decimal decimal Number ( ' 123.456 ' );
$ a = new PrestaShop decimal decimal Number ( ' -123.456 ' );
// truncate / pad
$ a -> round ( 0 ); // '123'
$ a -> round ( 1 ); // '123.4'
$ a -> round ( 2 ); // '123.45'
$ a -> round ( 3 ); // '123.456'
$ a -> round ( 4 ); // '123.456'
$ b -> round ( 0 ); // '-123'
$ b -> round ( 1 ); // '-123.4'
$ b -> round ( 2 ); // '-123.45'
$ b -> round ( 3 ); // '-123.456'
$ b -> round ( 4 ); // '-123.456'
이 숫자에 10^지수를 곱한 새 복사본을 만듭니다.
public decimal Number:: toMagnitude ( int $ exponent ): decimal Number
예:
$ a = new PrestaShop decimal decimal Number ( ' 123.456789 ' );
// shift 3 digits to the left
$ a -> toMagnitude (- 3 ); // 0.123456789
// shift 3 digits to the right
$ a -> toMagnitude ( 3 ); // 123456.789
$ number = new PrestaShop decimal decimal Number ( ' 123.45 ' );
$ number -> getIntegerPart (); // '123'
$ number -> getFractionalPart (); // '45'
$ number -> getPrecision (); // '2' (number of decimal s)
$ number -> getSign (); // '' ('-' if the number was negative)
$ number -> getExponent (); // '2' (always positive)
$ number -> getCoefficient (); // '123456'
$ number -> isPositive (); // true
$ number -> isNegative (); // false
$ number -> invert (); // new decimal decimal Number('-123.45')
$ composer install
$ vendor/bin/phpunit
자세한 내용은 CONTRIBUTING을 참조하세요.
MIT 라이센스(MIT). 자세한 내용은 라이센스 파일을 참조하십시오.