diff --git a/bignumber.d.ts b/bignumber.d.ts index 23bf9dc..a8ca624 100644 --- a/bignumber.d.ts +++ b/bignumber.d.ts @@ -109,7 +109,7 @@ export namespace BigNumber { * Calling `toString` with a base argument, e.g. `toString(10)`, will also always return normal * notation. */ - EXPONENTIAL_AT?: number|[number, number]; + EXPONENTIAL_AT?: number | [number, number]; /** * An integer, magnitude 1 to 1e+9, or an array, [-1e+9 to -1, 1 to 1e+9]. @@ -144,7 +144,7 @@ export namespace BigNumber { * The largest possible magnitude of a finite BigNumber is 9.999...e+1000000000. * The smallest possible magnitude of a non-zero BigNumber is 1e-1000000000. */ - RANGE?: number|[number, number]; + RANGE?: number | [number, number]; /** * A boolean: `true` or `false`. Default value: `false`. @@ -330,10 +330,29 @@ export namespace BigNumber { suffix?: string; } + interface RegularNumberObject { + /** + * The coefficient of the value of this BigNumber, an array of base 1e14 integer numbers. + */ + readonly c: number[]; + + /** + * The exponent of the value of this BigNumber, an integer number, -1000000000 to 1000000000. + */ + readonly e: number; + + /** + * The sign of the value of this BigNumber, -1 or 1. + */ + readonly s: number; + } + + type NaNObject = { readonly c: null, readonly e: null, readonly s: null }; + export type Object = RegularNumberObject | NaNObject; export type Instance = BigNumber; export type ModuloMode = 0 | 1 | 3 | 6 | 9; export type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; - export type Value = string | number | BigNumber; + export type Value = string | number | BigNumber | BigNumber.Object; } export declare class BigNumber { @@ -1057,7 +1076,7 @@ export declare class BigNumber { * @param n A numeric value. * @param [base] The base of n. */ - multipliedBy(n: BigNumber.Value, base?: number) : BigNumber; + multipliedBy(n: BigNumber.Value, base?: number): BigNumber; /** * Returns a BigNumber whose value is the value of this BigNumber multiplied by `n`. diff --git a/bignumber.js b/bignumber.js index e7a7fe7..ca980d9 100644 --- a/bignumber.js +++ b/bignumber.js @@ -195,7 +195,8 @@ if (b == null) { // Duplicate. - if (n instanceof BigNumber) { + if (n instanceof BigNumber + || (n && typeof n.s !== 'undefined' && typeof n.e !== 'undefined' && typeof n.c !== 'undefined')) { x.s = n.s; x.e = n.e; x.c = (n = n.c) ? n.slice() : n; diff --git a/doc/API.html b/doc/API.html index c721b14..8f2f00c 100644 --- a/doc/API.html +++ b/doc/API.html @@ -185,7 +185,7 @@
BigNumberBigNumber(n [, base]) ⇒ BigNumber

- n: number|string|BigNumber
+ n: number|string|BigNumber|BigNumberData
base: number: integer, 2 to 36 inclusive. (See ALPHABET to extend this range).

@@ -268,6 +268,10 @@
new BigNumber(823456789123456.3) // '[BigNumber Error] Not a base 2 number' new BigNumber(9, 2) +

+ As a way to interact with other modules that are using BigNumber you can use BigNumberData directly +

+
new BigNumber({ s: 1, e: 2, c: [ 777, 12300000000000 ] })    // '777.123'
diff --git a/test/methods/BigNumber.js b/test/methods/BigNumber.js index 9644a9d..3bbac1b 100644 --- a/test/methods/BigNumber.js +++ b/test/methods/BigNumber.js @@ -199,6 +199,10 @@ Test('bigNumber', function () { t('-102', new BigNumber('-0o146').toString()); t('0.5', new BigNumber('0o0.4').toString()); + t('100002222.2222333322', new BigNumber({ s: 1, e: 8, c: [100002222, 22223333220000] }).toString()) + t('7777777777.123123123', new BigNumber({ s: 1, e: 9, c: [7777777777, 12312312300000] }).toString()) + t('NaN', new BigNumber({ s: null, e: null, c: null }).toString()) + // Base-conversion tests //var alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_';