1 | ;
|
2 |
|
3 | var deepMap = require('./../utils/collection/deepMap');
|
4 |
|
5 | function factory(type, config, load, typed) {
|
6 | /**
|
7 | * Create a number or convert a string, boolean, or unit to a number.
|
8 | * When value is a matrix, all elements will be converted to number.
|
9 | *
|
10 | * Syntax:
|
11 | *
|
12 | * math.number(value)
|
13 | * math.number(unit, valuelessUnit)
|
14 | *
|
15 | * Examples:
|
16 | *
|
17 | * math.number(2) // returns number 2
|
18 | * math.number('7.2') // returns number 7.2
|
19 | * math.number(true) // returns number 1
|
20 | * math.number([true, false, true, true]) // returns [1, 0, 1, 1]
|
21 | * math.number(math.unit('52cm'), 'm') // returns 0.52
|
22 | *
|
23 | * See also:
|
24 | *
|
25 | * bignumber, boolean, complex, index, matrix, string, unit
|
26 | *
|
27 | * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted
|
28 | * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number
|
29 | * @return {number | Array | Matrix} The created number
|
30 | */
|
31 | var number = typed('number', {
|
32 | '': function _() {
|
33 | return 0;
|
34 | },
|
35 | 'number': function number(x) {
|
36 | return x;
|
37 | },
|
38 | 'string': function string(x) {
|
39 | if (x === 'NaN') return NaN;
|
40 | var num = Number(x);
|
41 |
|
42 | if (isNaN(num)) {
|
43 | throw new SyntaxError('String "' + x + '" is no valid number');
|
44 | }
|
45 |
|
46 | return num;
|
47 | },
|
48 | 'BigNumber': function BigNumber(x) {
|
49 | return x.toNumber();
|
50 | },
|
51 | 'Fraction': function Fraction(x) {
|
52 | return x.valueOf();
|
53 | },
|
54 | 'Unit': function Unit(x) {
|
55 | throw new Error('Second argument with valueless unit expected');
|
56 | },
|
57 | 'null': function _null(x) {
|
58 | return 0;
|
59 | },
|
60 | 'Unit, string | Unit': function UnitStringUnit(unit, valuelessUnit) {
|
61 | return unit.toNumber(valuelessUnit);
|
62 | },
|
63 | 'Array | Matrix': function ArrayMatrix(x) {
|
64 | return deepMap(x, number);
|
65 | }
|
66 | });
|
67 | number.toTex = {
|
68 | 0: "0",
|
69 | 1: "\\left(${args[0]}\\right)",
|
70 | 2: "\\left(\\left(${args[0]}\\right)${args[1]}\\right)"
|
71 | };
|
72 | return number;
|
73 | }
|
74 |
|
75 | exports.name = 'number';
|
76 | exports.factory = factory; |
\ | No newline at end of file |