FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mathjs/src/type/number.js at develop · rpeg/mathjs · GitHub
rpeg
/
mathjs
Public
forked from
josdejong/mathjs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
mathjs
/
src
/
type
/
number.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (66 loc) · 2 KB
Breadcrumbs
mathjs
/
src
/
type
/
number.js
Copy path
File metadata and controls
77 lines (66 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import
{
factory
}
from
'../utils/factory'
import
{
deepMap
}
from
'../utils/collection'
const
name
=
'number'
const
dependencies
=
[
'typed'
]
export
const
createNumber
=
/* #__PURE__ */
factory
(
name
,
dependencies
,
(
{
typed
}
)
=>
{
/**
* Create a number or convert a string, boolean, or unit to a number.
* When value is a matrix, all elements will be converted to number.
*
* Syntax:
*
* math.number(value)
* math.number(unit, valuelessUnit)
*
* Examples:
*
* math.number(2) // returns number 2
* math.number('7.2') // returns number 7.2
* math.number(true) // returns number 1
* math.number([true, false, true, true]) // returns [1, 0, 1, 1]
* math.number(math.unit('52cm'), 'm') // returns 0.52
*
* See also:
*
* bignumber, boolean, complex, index, matrix, string, unit
*
*
@param
{
string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null
} [value] Value to be converted
*
@param
{
Unit | string
} [valuelessUnit] A valueless unit, used to convert a unit to a number
*
@return
{
number | Array | Matrix
} The created number
*/
const
number
=
typed
(
'number'
,
{
''
:
function
(
)
{
return
0
}
,
number
:
function
(
x
)
{
return
x
}
,
string
:
function
(
x
)
{
if
(
x
===
'NaN'
)
return
NaN
const
num
=
Number
(
x
)
if
(
isNaN
(
num
)
)
{
throw
new
SyntaxError
(
'String "'
+
x
+
'" is no valid number'
)
}
return
num
}
,
BigNumber
:
function
(
x
)
{
return
x
.
toNumber
(
)
}
,
Fraction
:
function
(
x
)
{
return
x
.
valueOf
(
)
}
,
Unit
:
function
(
x
)
{
throw
new
Error
(
'Second argument with valueless unit expected'
)
}
,
null
:
function
(
x
)
{
return
0
}
,
'Unit, string | Unit'
:
function
(
unit
,
valuelessUnit
)
{
return
unit
.
toNumber
(
valuelessUnit
)
}
,
'Array | Matrix'
:
function
(
x
)
{
return
deepMap
(
x
,
number
)
}
}
)
return
number
}
)
Back
|
FazBrowse Home
|
New Git URL