-
Notifications
You must be signed in to change notification settings - Fork 741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement polyfill for TC39 bigdecimal #256
Comments
Okay, here's a preliminary polyfill implemented from a severely hacked decimal.js: The API is as described at the TC39 Decimal proposal and the Decimal: For Stage 1 slideshow. The zip file contains an ES module version and an ES 3+ compatible UMD version., Example usage: import BigDecimal from './bigdecimal.min.mjs';
let a, b, c, options;
a = BigDecimal(0.1);
b = BigDecimal('0.1');
console.log( a['==='](b) ) // false
console.log(a.toString()); // '0.1000000000000000055511151231257827021181583404541015625'
console.log(b.toString()); // '0.1'
console.log( a['!=='](b) ); // true
console.log( a['=='](b) ); // false
console.log( a['!='](b) ); // true
console.log( a['>'](b) ); // true
console.log( a['>='](b) ); // true
console.log( a['<'](b) ); // false
console.log( a['<='](b) ); // false
console.log( a['+'](b).toString() ); // '0.2000000000000000055511151231257827021181583404541015625'
c = a['-'](b);
console.log( c.toString() ); // '5.5511151231257827021181583404541015625e-18'
console.log( c.toFixed() ); // '0.0000000000000000055511151231257827021181583404541015625'
console.log( a['*'](b).toString() ); // '0.01000000000000000055511151231257827021181583404541015625'
a = BigDecimal('34.63');
b = BigDecimal('2.2343465');
c = BigDecimal(b);
console.log( b['==='](c) );
console.log( a['%'](b).toString() );
options = { maximumSignificantDigits: 20, roundingMode: BigDecimal.ROUND_HALF_UP };
console.log( BigDecimal.add(a, b, options).toString() );
console.log( BigDecimal.sub(a, b, options).toString() );
console.log( BigDecimal.mul(a, b, options).toString() );
console.log( BigDecimal.div(a, b, options).toString() );
console.log( BigDecimal.mod(a, b, options).toString() );
console.log( BigDecimal.pow(a, BigDecimal(11), options).toString() );
options = { maximumFractionDigits: 2, roundingMode: BigDecimal.ROUND_UP };
console.log( BigDecimal.round(a, options).toString() );
console.log( b.toExponential() );
console.log( b.toExponential(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) );
console.log( b.toFixed(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) );
console.log( b.toPrecision(4, { roundingMode: BigDecimal.ROUND_HALF_UP }) ); I haven't implemented a |
Ping tc39/proposal-decimal#17 for input |
@MikeMcl do you have a non-minified version checked in somewhere? |
Yes, I have a non-minified version. I don't want to expose it before I've had a chance to clean it up a bit. |
See:
tc39/proposal-decimal#17
(polyfill)
and:
tc39/proposal-decimal#18
(babel)
The text was updated successfully, but these errors were encountered: