Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 45678901234567890123456789012345678901234567890123456789012345678901234567890
- // 14 March 2019
- // 18 March 2019 - support expontent notation.
- // 19 March 2019 - better comment style, better return style.
- /**
- * @function fraction accepts param of any type and returns the decimal fraction
- * of the value as a string, if the value is coercible to a number.
- *
- * If the coerced value results in null, undefined, or NaN, the function returns
- * "0".
- *
- * Function accepts values in exponent notation and will insert leading '0' for
- * negative decimals. For example, '9.123456789012345e-21' will result in
- * '.000000000000000000009123456789012345', and '555.1111e-10' will result in
- * '.00000005551111'.
- *
- * @param {*} num
- * @returns {string} fractional value
- */
- function fraction(num) {
- var s = String(num);
- var d = s.match(/\./g) || [];
- var n = d.length > 1 ? '0' : s;
- var m = n.split('.')[1];
- var e = m ? m.split('e')[1] : 0;
- if (!e) {
- /*
- * Case 1: No exponent notation, so handle in normal course.
- * If null or NaN, return 0, else return mantissa.
- */
- return m == null || m !== m ? '0' : '.' + m;
- }
- if (+e >= 0) {
- /*
- * Case 2: An exponent notation has been found, its value is non-negative,
- * so there can only be 0's to the right.
- */
- return '0';
- }
- /*
- * Case 3: An exponent notation has been found, its value is negative, so
- * insert 0's between the decimal and the value before 'e'.
- */
- // Note that the tilde operator converts, e.g., -8 to +7.
- var a = Array(~+e).fill(0).join('');
- var r = s.split('e')[0].match(/\d/g).join('');
- return '.' + a + r;
- }
- var tests = [
- 1, 2, 3.4, 1234.5678,
- '1,4567', NaN, null,
- undefined, , 'a', true,
- 'truth', 'true.9.false',
- Math, Math.round,
- '12312231231231232112.2456789',
- Number.POSITIVE_INFINITY,
- Number.EPSILON,
- 9.11111111015e20,
- 9.11111111015e21,
- 9.11111111015e31,
- 9.1234567890123456789e-21,
- 9.11111111015e-10,
- 0.00001111e-10,
- 555.1111e-10
- ];
- var results = tests.map(function(test) {
- return { input: test, result: fraction(test) };
- });
- console.log(JSON.stringify(results, null, 2));
- /*
- [
- {
- "input": 1,
- "result": "0"
- },
- {
- "input": 2,
- "result": "0"
- },
- {
- "input": 3.4,
- "result": ".4"
- },
- {
- "input": 1234.5678,
- "result": ".5678"
- },
- {
- "input": "1,4567",
- "result": "0"
- },
- {
- "input": null,
- "result": "0"
- },
- {
- "input": null,
- "result": "0"
- },
- {
- "result": "0"
- },
- null,
- {
- "input": "a",
- "result": "0"
- },
- {
- "input": true,
- "result": "0"
- },
- {
- "input": "truth",
- "result": "0"
- },
- {
- "input": "true.9.false",
- "result": "0"
- },
- {
- "input": {},
- "result": "0"
- },
- {
- "result": "0"
- },
- {
- "input": "12312231231231232112.2456789",
- "result": ".2456789"
- },
- {
- "input": null,
- "result": "0"
- },
- {
- "input": 2.220446049250313e-16,
- "result": ".0000000000000002220446049250313"
- },
- {
- "input": 911111111015000000000,
- "result": "0"
- },
- {
- "input": 9.11111111015e+21,
- "result": "0"
- },
- {
- "input": 9.11111111015e+31,
- "result": "0"
- },
- {
- "input": 9.123456789012345e-21,
- "result": ".000000000000000000009123456789012345"
- },
- {
- "input": 9.11111111015e-10,
- "result": ".000000000911111111015"
- },
- {
- "input": 1.111e-15,
- "result": ".000000000000001111"
- },
- {
- "input": 5.551111e-8,
- "result": ".00000005551111"
- }
- ]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement