Proposal: 1) Number (integer or decimal) to Array 2) Array to Number (integer or decimal)
Tab Atkins Jr.
jackalmage at gmail.com
Sun Mar 10 15:57:52 UTC 2019
On Sat, Mar 9, 2019 at 11:10 AM Felipe Nascimento de Moura
<felipenmoura at gmail.com> wrote:
> Personally, I don't think it would be THAT useful...
> but...I think there is something behind this proposal that makes sense.
> I do believe it could be useful for developers to have an easier access to number parts or characteristics.
> Perhaps something like:
> const i = 1234.567;
Can you provide a scenario in which these would do something useful,
such that it would be worth adding them over just using the math
operations that already exist?
> console.log( i.float ); // 567
i % 1
(If you really wanted this as an integer, it's not well-founded; .567
isn't exactly representable as a double, so JS doesn't know that you
"meant" it to have only three digits after the decimal point, and thus
want 567 as the answer. You'll instead get some very very large
integer that *starts with* 567, followed by a bunch of zeros, followed
by some randomish digits at the end.)
> console.log( i.abs ); // 1234
> console.log( i.thousands ); // 1
Math.trunc(i / 1000)
> console.log( i.million ); // 0
Math.trunc(i / 1e6)
> console.log( i.hundred ); // 2
Math.trunc(i / 100) % 10
> console.log( i.hundreds ); // 12
Math.trunc(i / 100)
> console.log( i.ten ); // 2
Math.trunc(i / 10) % 10
> console.log( i.tens ); // 123
Math.trunc(i / 10)
> console.log( i.tenth ); // 5
Math.trunc(i % 1 * 10) % 10
> console.log( i.tenths ); // 5
Math.trunc(i % 1 * 10)
> console.log( i.hundredth ); // 6
Math.trunc(i % 1 * 100) % 10
> console.log( i.hundredths ); // 56
Math.trunc(i % 1 * 100)
Some of these are easy to remember and use; others take some thinking
to deploy. But the question is still "what would someone use this
information for?", such that the benefit to developers is worth the
cost to all parties involved (spec writers, implementors, testers, and
then developers having to navigate a larger stdlib).
More information about the es-discuss