• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Mar 19th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // 45678901234567890123456789012345678901234567890123456789012345678901234567890
2. // 14 March 2019
3. // 18 March 2019 - support expontent notation.
4. // 19 March 2019 - better comment style, better return style.
5.
6. /**
7.  * @function fraction accepts param of any type and returns the decimal fraction
8.  * of the value as a string, if the value is coercible to a number.
9.  *
10.  * If the coerced value results in null, undefined, or NaN, the function returns
11.  * "0".
12.  *
13.  * Function accepts values in exponent notation and will insert leading '0' for
14.  * negative decimals. For example, '9.123456789012345e-21' will result in
15.  * '.000000000000000000009123456789012345', and '555.1111e-10' will result in
16.  * '.00000005551111'.
17.  *
18.  * @param {*} num
19.  * @returns {string} fractional value
20.  */
21.
22. function fraction(num) {
23.   var s = String(num);
24.   var d = s.match(/\./g) || [];
25.   var n = d.length > 1 ? '0' : s;
26.   var m = n.split('.')[1];
27.   var e = m ? m.split('e')[1] : 0;
28.
29.   if (!e) {
30.
31.     /*
32.      * Case 1: No exponent notation, so handle in normal course.
33.      * If null or NaN, return 0, else return mantissa.
34.      */
35.
36.     return m == null || m !== m ? '0' : '.' + m;
37.   }
38.
39.
40.   if (+e >= 0) {
41.     /*
42.      * Case 2: An exponent notation has been found, its value is non-negative,
43.      * so there can only be 0's to the right.
44.      */
45.
46.     return '0';
47.   }
48.
49.   /*
50.    * Case 3: An exponent notation has been found, its value is negative, so
51.    * insert 0's between the decimal and the value before 'e'.
52.    */
53.
54.   // Note that the tilde operator converts, e.g., -8 to +7.
55.   var a = Array(~+e).fill(0).join('');
56.   var r = s.split('e')[0].match(/\d/g).join('');
57.
58.   return '.' + a + r;
59. }
60.
61. var tests = [
62.   1, 2, 3.4, 1234.5678,
63.   '1,4567', NaN, null,
64.   undefined, , 'a', true,
65.   'truth', 'true.9.false',
66.   Math, Math.round,
67.   '12312231231231232112.2456789',
68.   Number.POSITIVE_INFINITY,
69.   Number.EPSILON,
70.   9.11111111015e20,
71.   9.11111111015e21,
72.   9.11111111015e31,
73.   9.1234567890123456789e-21,
74.   9.11111111015e-10,
75.   0.00001111e-10,
76.   555.1111e-10
77. ];
78.
79. var results = tests.map(function(test) {
80.   return { input: test, result: fraction(test) };
81. });
82.
83. console.log(JSON.stringify(results, null, 2));
84.
85. /*
86. [
87.   {
88.     "input": 1,
89.     "result": "0"
90.   },
91.   {
92.     "input": 2,
93.     "result": "0"
94.   },
95.   {
96.     "input": 3.4,
97.     "result": ".4"
98.   },
99.   {
100.     "input": 1234.5678,
101.     "result": ".5678"
102.   },
103.   {
104.     "input": "1,4567",
105.     "result": "0"
106.   },
107.   {
108.     "input": null,
109.     "result": "0"
110.   },
111.   {
112.     "input": null,
113.     "result": "0"
114.   },
115.   {
116.     "result": "0"
117.   },
118.   null,
119.   {
120.     "input": "a",
121.     "result": "0"
122.   },
123.   {
124.     "input": true,
125.     "result": "0"
126.   },
127.   {
128.     "input": "truth",
129.     "result": "0"
130.   },
131.   {
132.     "input": "true.9.false",
133.     "result": "0"
134.   },
135.   {
136.     "input": {},
137.     "result": "0"
138.   },
139.   {
140.     "result": "0"
141.   },
142.   {
143.     "input": "12312231231231232112.2456789",
144.     "result": ".2456789"
145.   },
146.   {
147.     "input": null,
148.     "result": "0"
149.   },
150.   {
151.     "input": 2.220446049250313e-16,
152.     "result": ".0000000000000002220446049250313"
153.   },
154.   {
155.     "input": 911111111015000000000,
156.     "result": "0"
157.   },
158.   {
159.     "input": 9.11111111015e+21,
160.     "result": "0"
161.   },
162.   {
163.     "input": 9.11111111015e+31,
164.     "result": "0"
165.   },
166.   {
167.     "input": 9.123456789012345e-21,
168.     "result": ".000000000000000000009123456789012345"
169.   },
170.   {
171.     "input": 9.11111111015e-10,
172.     "result": ".000000000911111111015"
173.   },
174.   {
175.     "input": 1.111e-15,
176.     "result": ".000000000000001111"
177.   },
178.   {
179.     "input": 5.551111e-8,
180.     "result": ".00000005551111"
181.   }
182. ]
183. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top