class DecimalConverter implements Converter (View source)

Converts numbers by using a mathematical algorithm that relies on integers.

DecimalConverter employs arbitrary-precision integer arithmetic to first convert the digits to decimal system and then to convert the digits to the target base. DecimalConverter depends on the GMP extension to perform the required arbitrary precision integer calculations.

Methods

__construct(NumberBase $source, NumberBase $target)

Creates a new instance of DecimalConverter.

void
setPrecision(int $precision)

Sets the precision for inaccurate fraction conversions.

array
convertInteger(array $number)

Converts the integer part of a number.

array
convertFractions(array $number)

Converts the fractional part of a number.

Details

__construct(NumberBase $source, NumberBase $target)

Creates a new instance of DecimalConverter.

Parameters

NumberBase $source Number base used by the provided numbers
NumberBase $target Number base used by the returned numbers

void setPrecision(int $precision)

Sets the precision for inaccurate fraction conversions.

The fractions cannot always be accurately converted from base to another, since they may represent a fraction that cannot be represented in another number base. The precision value is used to determine the number of digits in the fractional part, if the number cannot be accurately converted or if it is not feasible to determine that.

If the precision is positive, it defines the maximum number of digits in the fractional part. If the value is 0, the converted number will have at least as many digits in the fractional part as it takes to represent the number in the same accuracy as the original number. A negative number will simply increase the number of digits.

Note that the fractional part may have fewer digits than what is required by the precision if it can be accurately represented using fewer digits.

Parameters

int $precision Precision used for inaccurate conversions

Return Value

void

array convertInteger(array $number)

Converts the integer part of a number.

The integer part should be provided as an array of digits with least significant digit first. Any invalid digit in the array will cause an exception to be thrown. The return value will be a similar array of digits, except converted to the target number base.

Parameters

array $number Array of digits representing the integer part

Return Value

array Digits for the converted number

Exceptions

InvalidDigitException If the integer part contains invalid digits

array convertFractions(array $number)

Converts the fractional part of a number.

The fractional part should be provided as an array of digits with least significant digit first. Any invalid digit in the array will cause an exception to be thrown. The return value will be a similar array of digits, except converted to the target number base.

Parameters

array $number Array of digits representing the fractional part

Return Value

array Digits for the converted number

Exceptions

InvalidDigitException If the fractional part contain invalid digits