mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
Added toRadixString method
This commit is contained in:
parent
fb37ca6bde
commit
abf5f3ded5
@ -1,3 +1,7 @@
|
||||
####6.0.1
|
||||
* 20/09/2016
|
||||
* Added toRadixString() method.
|
||||
|
||||
####6.0.0
|
||||
* 30/06/2016
|
||||
* Removed base-88 serialization format.
|
||||
|
29
decimal.js
29
decimal.js
@ -24,7 +24,7 @@
|
||||
MAX_DIGITS = 1e9, // 0 to 1e9
|
||||
|
||||
// Base conversion alphabet.
|
||||
NUMERALS = '0123456789abcdef',
|
||||
NUMERALS = '0123456789abcdefghijklmnopqrstuvwxyz',
|
||||
|
||||
// The natural logarithm of 10 (1025 digits).
|
||||
LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',
|
||||
@ -2206,7 +2206,26 @@
|
||||
P.toOctal = function (sd, rm) {
|
||||
return toStringBinary(this, 8, sd, rm);
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Return a string representing the value of this Decimal in given base (between 2 and 36),
|
||||
* round to `sd` significant digits using rounding mode `rm`.
|
||||
*
|
||||
* If the optional `sd` argument is present then return binary exponential notation.
|
||||
*
|
||||
* [radix] {number} Radix of the string to export to
|
||||
* [sd] {number} Significant digits. Integer, 1 to MAX_DIGITS inclusive.
|
||||
* [rm] {number} Rounding mode. Integer, 0 to [radix] inclusive.
|
||||
*
|
||||
*/
|
||||
P.toRadixString = function (radix, sd, rm) {
|
||||
radix = parseInt(radix, 10);
|
||||
if (radix < 2) { radix = 2; }
|
||||
if (radix > 36) { radix = 36; }
|
||||
return toStringBinary(this, radix, sd, rm, true);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Return a new Decimal whose value is the value of this Decimal raised to the power `y`, rounded
|
||||
@ -3761,7 +3780,7 @@
|
||||
*
|
||||
* If the optional `sd` argument is present include a binary exponent suffix.
|
||||
*/
|
||||
function toStringBinary(x, baseOut, sd, rm) {
|
||||
function toStringBinary(x, baseOut, sd, rm, skipPrefix) {
|
||||
var base, e, i, k, len, roundUp, str, xd, y,
|
||||
Ctor = x.constructor,
|
||||
isExp = sd !== void 0;
|
||||
@ -3886,7 +3905,9 @@
|
||||
}
|
||||
}
|
||||
|
||||
str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str;
|
||||
if (!skipPrefix) {
|
||||
str = (baseOut == 16 ? '0x' : baseOut == 2 ? '0b' : baseOut == 8 ? '0o' : '') + str;
|
||||
}
|
||||
}
|
||||
|
||||
return x.s < 0 ? '-' + str : str;
|
||||
|
2
decimal.min.js
vendored
2
decimal.min.js
vendored
File diff suppressed because one or more lines are too long
41
doc/API.html
41
doc/API.html
@ -187,6 +187,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<li><a href="#toOctal" >toOctal </a> </li>
|
||||
<li><a href="#pow" >toPower </a><span>pow</span> </li>
|
||||
<li><a href="#toPrecision" >toPrecision </a> </li>
|
||||
<li><a href="#toRadixString">toRadixString </a> </li>
|
||||
<li><a href="#toSD" >toSignificantDigits </a><span>toSD</span> </li>
|
||||
<li><a href="#toString" >toString </a> </li>
|
||||
<li><a href="#trunc" >truncated </a><span>trunc</span> </li>
|
||||
@ -2362,7 +2363,45 @@ y.toPrecision(5) // '45.600'</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="toSD">
|
||||
<h5 id="toRadixString">toRadixString<code class='inset'>.toRadixString(base[, sd [, rm]]) <i>⇒ string</i></code></h5>
|
||||
<p>Returns a string representing the value of this Decimal in given base between 2 and 36, rounded to <code>sd</code>
|
||||
significant digits using rounding mode <code>rm</code>.
|
||||
</p>
|
||||
<p>
|
||||
If <code>sd</code> is defined, the return value will use binary exponential notation.
|
||||
</p>
|
||||
<p>
|
||||
If <code>sd</code> is omitted, the return value will be rounded to
|
||||
<a href='#precision'><code>precision</code></a> significant digits.
|
||||
</p>
|
||||
<p>
|
||||
If <code>rm</code> is omitted, rounding mode <a href='#rounding'><code>rounding</code></a>
|
||||
will be used.
|
||||
</p>
|
||||
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.
|
||||
<code>base</code> values below 2 are treated as 2 and above 36 as 36.
|
||||
Non-integer values of <code>base</code> are floored to integer.</p>
|
||||
<p>
|
||||
If this Decimal has a positive exponent that is equal to or greater than
|
||||
<a href="#toExpPos"><code>toExpPos</code></a>, or a negative exponent equal to or less than
|
||||
<a href="#toExpPos"><code>toExpNeg</code></a>, then exponential notation will be returned.
|
||||
</p>
|
||||
<pre>
|
||||
x = new Decimal(100)
|
||||
x.toRadixString(1) // '1100100'
|
||||
x.toRadixString(2) // '1100100'
|
||||
x.toRadixString(3) // '10201'
|
||||
x.toRadixString(4) // '1210'
|
||||
x.toRadixString(8) // '144'
|
||||
x.toRadixString(10) // '100'
|
||||
x.toRadixString(11) // '91'
|
||||
x.toRadixString(16) // '64'
|
||||
x.toRadixString(36) // '2s'
|
||||
x.toRadixString(37) // '2s'</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="toSD">
|
||||
toSignificantDigits<code class='inset'>.toSD([sd [, rm]]) <i>⇒ Decimal</i></code>
|
||||
</h5>
|
||||
<p>
|
||||
|
File diff suppressed because one or more lines are too long
221
test/modules/toRadixString.js
Normal file
221
test/modules/toRadixString.js
Normal file
@ -0,0 +1,221 @@
|
||||
if (typeof T === 'undefined') require('../setup');
|
||||
|
||||
T('toRadixString', function () {
|
||||
|
||||
function t(expected, n, radix) {
|
||||
T.assertEqual(expected, new Decimal(n).toRadixString(radix));
|
||||
}
|
||||
|
||||
Decimal.config({
|
||||
precision: 30,
|
||||
rounding: 4,
|
||||
toExpNeg: -9e15,
|
||||
toExpPos: 9e15,
|
||||
minE: -9e15,
|
||||
maxE: 9e15
|
||||
});
|
||||
|
||||
for (var rad = 2; rad <= 36; rad++) {
|
||||
t('0', 0, rad);
|
||||
t('0', '0', rad);
|
||||
t('NaN', NaN, rad);
|
||||
t('NaN', 'NaN', rad);
|
||||
t('Infinity', 1/0, rad);
|
||||
t('Infinity', 'Infinity', rad);
|
||||
t('1', 1, rad);
|
||||
}
|
||||
|
||||
t("1100100", 100, -1);
|
||||
t("1100100", 100, 0);
|
||||
t("1100100", 100, 0.1);
|
||||
t("1100100", 100, 1);
|
||||
t("2s", 100, 36.1);
|
||||
t("2s", 100, 37);
|
||||
t("2s", 100, 100);
|
||||
t("2s", 100, 10000);
|
||||
t("10201", 100, 3.01);
|
||||
t("10201", 100, 3.49);
|
||||
t("10201", 100, 3.5);
|
||||
t("10201", 100, 3.99);
|
||||
|
||||
t("1100100", 100, 2);
|
||||
t("10201", 100, 3);
|
||||
t("1210", 100, 4);
|
||||
t("400", 100, 5);
|
||||
t("244", 100, 6);
|
||||
t("202", 100, 7);
|
||||
t("144", 100, 8);
|
||||
t("121", 100, 9);
|
||||
t("100", 100, 10);
|
||||
t("91", 100, 11);
|
||||
t("84", 100, 12);
|
||||
t("79", 100, 13);
|
||||
t("72", 100, 14);
|
||||
t("6a", 100, 15);
|
||||
t("64", 100, 16);
|
||||
t("5f", 100, 17);
|
||||
t("5a", 100, 18);
|
||||
t("55", 100, 19);
|
||||
t("50", 100, 20);
|
||||
t("4g", 100, 21);
|
||||
t("4c", 100, 22);
|
||||
t("48", 100, 23);
|
||||
t("44", 100, 24);
|
||||
t("40", 100, 25);
|
||||
t("3m", 100, 26);
|
||||
t("3j", 100, 27);
|
||||
t("3g", 100, 28);
|
||||
t("3d", 100, 29);
|
||||
t("3a", 100, 30);
|
||||
t("37", 100, 31);
|
||||
t("34", 100, 32);
|
||||
t("31", 100, 33);
|
||||
t("2w", 100, 34);
|
||||
t("2u", 100, 35);
|
||||
t("2s", 100, 36);
|
||||
|
||||
t("10011100010000", 10000, 2);
|
||||
t("111201101", 10000, 3);
|
||||
t("2130100", 10000, 4);
|
||||
t("310000", 10000, 5);
|
||||
t("114144", 10000, 6);
|
||||
t("41104", 10000, 7);
|
||||
t("23420", 10000, 8);
|
||||
t("14641", 10000, 9);
|
||||
t("10000", 10000, 10);
|
||||
t("7571", 10000, 11);
|
||||
t("5954", 10000, 12);
|
||||
t("4723", 10000, 13);
|
||||
t("3904", 10000, 14);
|
||||
t("2e6a", 10000, 15);
|
||||
t("2710", 10000, 16);
|
||||
t("20a4", 10000, 17);
|
||||
t("1cfa", 10000, 18);
|
||||
t("18d6", 10000, 19);
|
||||
t("1500", 10000, 20);
|
||||
t("11e4", 10000, 21);
|
||||
t("kec", 10000, 22);
|
||||
t("iki", 10000, 23);
|
||||
t("h8g", 10000, 24);
|
||||
t("g00", 10000, 25);
|
||||
t("ekg", 10000, 26);
|
||||
t("dja", 10000, 27);
|
||||
t("cl4", 10000, 28);
|
||||
t("bpo", 10000, 29);
|
||||
t("b3a", 10000, 30);
|
||||
t("aci", 10000, 31);
|
||||
t("9og", 10000, 32);
|
||||
t("961", 10000, 33);
|
||||
t("8m4", 10000, 34);
|
||||
t("85p", 10000, 35);
|
||||
t("7ps", 10000, 36);
|
||||
|
||||
t("11110100001001000000", 1000000, 2);
|
||||
t("1212210202001", 1000000, 3);
|
||||
t("3310021000", 1000000, 4);
|
||||
t("224000000", 1000000, 5);
|
||||
t("33233344", 1000000, 6);
|
||||
t("11333311", 1000000, 7);
|
||||
t("3641100", 1000000, 8);
|
||||
t("1783661", 1000000, 9);
|
||||
t("1000000", 1000000, 10);
|
||||
t("623351", 1000000, 11);
|
||||
t("402854", 1000000, 12);
|
||||
t("290221", 1000000, 13);
|
||||
t("1c0608", 1000000, 14);
|
||||
t("14b46a", 1000000, 15);
|
||||
t("f4240", 1000000, 16);
|
||||
t("bg939", 1000000, 17);
|
||||
t("9987a", 1000000, 18);
|
||||
t("7cf1b", 1000000, 19);
|
||||
t("65000", 1000000, 20);
|
||||
t("52kc1", 1000000, 21);
|
||||
t("45k2c", 1000000, 22);
|
||||
t("3d486", 1000000, 23);
|
||||
t("3082g", 1000000, 24);
|
||||
t("2e000", 1000000, 25);
|
||||
t("24n7e", 1000000, 26);
|
||||
t("1nlk1", 1000000, 27);
|
||||
t("1hfe8", 1000000, 28);
|
||||
t("1c01m", 1000000, 29);
|
||||
t("1713a", 1000000, 30);
|
||||
t("12hi2", 1000000, 31);
|
||||
t("ugi0", 1000000, 32);
|
||||
t("rr91", 1000000, 33);
|
||||
t("pf1q", 1000000, 34);
|
||||
t("nbbf", 1000000, 35);
|
||||
t("lfls", 1000000, 36);
|
||||
|
||||
t("101111101011110000100000000", 100000000, 2);
|
||||
t("20222011112012201", 100000000, 3);
|
||||
t("11331132010000", 100000000, 4);
|
||||
t("201100000000", 100000000, 5);
|
||||
t("13531202544", 100000000, 6);
|
||||
t("2322662122", 100000000, 7);
|
||||
t("575360400", 100000000, 8);
|
||||
t("228145181", 100000000, 9);
|
||||
t("100000000", 100000000, 10);
|
||||
t("514a1531", 100000000, 11);
|
||||
t("295a6454", 100000000, 12);
|
||||
t("179437c9", 100000000, 13);
|
||||
t("d3d1212", 100000000, 14);
|
||||
t("8ba496a", 100000000, 15);
|
||||
t("5f5e100", 100000000, 16);
|
||||
t("42752cg", 100000000, 17);
|
||||
t("2ggadha", 100000000, 18);
|
||||
t("227675h", 100000000, 19);
|
||||
t("1b50000", 100000000, 20);
|
||||
t("13a3k7g", 100000000, 21);
|
||||
t("j8j9cc", 100000000, 22);
|
||||
t("fc7ll2", 100000000, 23);
|
||||
t("cd9j2g", 100000000, 24);
|
||||
t("a60000", 100000000, 25);
|
||||
t("8alepm", 100000000, 26);
|
||||
t("6q4e5j", 100000000, 27);
|
||||
t("5mjb0g", 100000000, 28);
|
||||
t("4pb61p", 100000000, 29);
|
||||
t("43dl3a", 100000000, 30);
|
||||
t("3f8m8e", 100000000, 31);
|
||||
t("2vbo80", 100000000, 32);
|
||||
t("2ialc1", 100000000, 33);
|
||||
t("26s96g", 100000000, 34);
|
||||
t("1vmcmu", 100000000, 35);
|
||||
t("1njchs", 100000000, 36);
|
||||
|
||||
t("1001010100000010111110010000000000", 10000000000, 2);
|
||||
t("221210220202122010101", 10000000000, 3);
|
||||
t("21110002332100000", 10000000000, 4);
|
||||
t("130440000000000", 10000000000, 5);
|
||||
t("4332142412144", 10000000000, 6);
|
||||
t("502544411644", 10000000000, 7);
|
||||
t("112402762000", 10000000000, 8);
|
||||
t("27726678111", 10000000000, 9);
|
||||
t("10000000000", 10000000000, 10);
|
||||
t("4271815011", 10000000000, 11);
|
||||
t("1b30b91054", 10000000000, 12);
|
||||
t("c349ba483", 10000000000, 13);
|
||||
t("6ac162c24", 10000000000, 14);
|
||||
t("3d7dace6a", 10000000000, 15);
|
||||
t("2540be400", 10000000000, 16);
|
||||
t("1764g6422", 10000000000, 17);
|
||||
t("g603fb9a", 10000000000, 18);
|
||||
t("b3abb909", 10000000000, 19);
|
||||
t("7g500000", 10000000000, 20);
|
||||
t("5bcaikk4", 10000000000, 21);
|
||||
t("40487b0c", 10000000000, 22);
|
||||
t("2lcfd6fg", 10000000000, 23);
|
||||
t("247kjf2g", 10000000000, 24);
|
||||
t("1fo00000", 10000000000, 25);
|
||||
t("169gphag", 10000000000, 26);
|
||||
t("plokh3a", 10000000000, 27);
|
||||
t("kl17a14", 10000000000, 28);
|
||||
t("gnfiqc6", 10000000000, 29);
|
||||
t("dlfkb3a", 10000000000, 30);
|
||||
t("b893q85", 10000000000, 31);
|
||||
t("9a0np00", 10000000000, 32);
|
||||
t("7oh8of1", 10000000000, 33);
|
||||
t("6g35112", 10000000000, 34);
|
||||
t("5fdv5ap", 10000000000, 35);
|
||||
t("4ldqpds", 10000000000, 36);
|
||||
|
||||
});
|
@ -74,6 +74,7 @@
|
||||
'toPrecision',
|
||||
'toSD',
|
||||
'toString',
|
||||
'toRadixString',
|
||||
'trunc',
|
||||
'valueOf',
|
||||
];
|
||||
|
@ -61,6 +61,7 @@ console.log('\n Testing decimal.js\n');
|
||||
'toPrecision',
|
||||
'toSD',
|
||||
'toString',
|
||||
'toRadixString',
|
||||
'trunc',
|
||||
'valueOf'
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user