-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.js.html
219 lines (180 loc) · 10.4 KB
/
number.js.html
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: number.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: number.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
数字相关
Thanks:Coco
*/
/**
* [getRoundNum description] 生成范围随机数
* @param min {Number} 最小值
* @param max {Number} 最大值
* @return {Number} 生成的随机数字
*/
function genRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;;
}
/**
* [getRoundNum description] 精确到小数点后几位
* @param num {Number} 原数值
* @param decimal {Number} 精确到小数点后的位数
* @return {Number} 精确后的数字
*/
function getRoundNum(num, decimal) {
return Math.round(num * 10 ** decimal) / 10 ** decimal;
}
/**
* [isDigit description] 判断是否为数字类型
* @param value {String/Number} 需要判断的值
* @return {String} 判断
*/
function isDigit(value) {
var patrn = /^[0-9]*$/;
if (patrn.exec(value) === null || value === "") {
return false
} else {
return true
}
}
/**
* [numOfComma description] 将数字转换为每3位添加一个逗号的格式,123456->123,456
* @param num {Number} 原数字
* @return {String} 转换后的"数字"
*/
function numOfComma(num) {
num = "" + num; //数字转换为字符串
var len = num.length,
commaNum = parseInt((len - 1) / 3),
leftNum = len % 3 === 0 ? 3 : len % 3,
result = "";
if (len <= 3) { //长度小于3
result = num;
} else {
result = num.slice(0, leftNum);
for (var i = commaNum; i >= 1; i--) {
result += "," + num.slice(len - i * 3, len - (i - 1) * 3);
}
}
return result;
}
/**
* [numOfComma2 description] 利用正则将数字转换为每3位添加一个逗号的格式(第2种实现方式)
* @param num {Number} 原数字
* @return {String} 转换后的"数字"
*/
function numOfComma2(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
/**
* [numberUpperFormat description] 数字转换,简单理解后面要有三个0,则是千,4个零,则是万。当然不一定是零,位数到了就行,反正都会省略掉(未做四舍五入),来自:http://xjinjin.net/2016/08/12/unit-conversion/
* @param input {Number} 原数字
* @return {String} 转换后的"数字"
*/
function numberUpperFormat(input) {
// 可以随意的增删改units内容实现单位的配置
var units = [
{ num: 3, unit: '千' },
{ num: 4, unit: '万' },
{ num: 6, unit: '百万' },
{ num: 7, unit: '千万' },
{ num: 8, unit: '亿' }];
// 精确到整数
var num = input.toFixed('0');
if (num.length <= 3) {
// 千以下不加单位
return num;
}
// 保证前面至少留一位
var len = num.length - 1;
var isFind = false;
for (var i = 0, length = units.length; i < length; i++) {
var item = units[i];
if (len >= item.num && len < units[i + 1].num) {
isFind = true;
} else if (i === length - 2) {
isFind = true;
item = units[++i];
}
if (isFind) {
// 确认区间后,返回前几位加上单位
return '' + num.slice(0, num.length - item.num) + item.unit;
}
}
}
/**
* [digitUppercase description] 金额的大写转换,来自:https://segmentfault.com/g/1570000011077131/d/1560000011078444
* @param n {Number} 原金额数字
* @return {String} 大写的金额
*/
function digitUppercase(n) {
var fraction = ['角', '分'];
var digit = [
'零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖'
];
var unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
];
var head = n < 0 ? '欠' : '';
n = Math.abs(n);
var s = '';
for (var i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
}
s = s || '整';
n = Math.floor(n);
for (var j = 0; j < unit[0].length && n > 0; j++) {
var p = '';
for (var k = 0; k < unit[1].length && n > 0; k++) {
p = digit[n % 10] + unit[1][k] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][j] + s;
}
return head + s.replace(/(零.)*零元/, '元')
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整');
}
/**
* [formatDuration description] 时间转换,多用于视频播放时的剩余时间显示
* @param t {Number/String} 视频的秒数
* @return {String} 转换后的时间,例如:'389:03','00:05'
*/
function formatDuration (t) {
if (!t) { return ''; }
var m = Math.floor(t / 60);
var s = t % 60;
if (m < 10) { m = '0' + m; }
if (s < 10) { s = '0' + s; }
return m + ':' + s;
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#addQuote">addQuote</a></li><li><a href="global.html#addStyle">addStyle</a></li><li><a href="global.html#appendQuery">appendQuery</a></li><li><a href="global.html#bs2">bs2</a></li><li><a href="global.html#bs4">bs4</a></li><li><a href="global.html#bs8">bs8</a></li><li><a href="global.html#byteCount">byteCount</a></li><li><a href="global.html#CanvasTest">CanvasTest</a></li><li><a href="global.html#cloneDeep">cloneDeep</a></li><li><a href="global.html#copyTextToClipboard">copyTextToClipboard</a></li><li><a href="global.html#countItemNum">countItemNum</a></li><li><a href="global.html#cssTest">cssTest</a></li><li><a href="global.html#dateToString">dateToString</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#digitUppercase">digitUppercase</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#escapeRegExp">escapeRegExp</a></li><li><a href="global.html#formatDate">formatDate</a></li><li><a href="global.html#formatDuration">formatDuration</a></li><li><a href="global.html#genDateArray">genDateArray</a></li><li><a href="global.html#generateDaysArray">generateDaysArray</a></li><li><a href="global.html#genRandomColor">genRandomColor</a></li><li><a href="global.html#genRandomHEX">genRandomHEX</a></li><li><a href="global.html#genRandomId">genRandomId</a></li><li><a href="global.html#genRandomNum">genRandomNum</a></li><li><a href="global.html#genRandomString">genRandomString</a></li><li><a href="global.html#genStarScore">genStarScore</a></li><li><a href="global.html#genTimeStamp">genTimeStamp</a></li><li><a href="global.html#getBrowserName">getBrowserName</a></li><li><a href="global.html#getCookie">getCookie</a></li><li><a href="global.html#getCurrentPath">getCurrentPath</a></li><li><a href="global.html#getDateDiff">getDateDiff</a></li><li><a href="global.html#getDevice">getDevice</a></li><li><a href="global.html#getInputIndex">getInputIndex</a></li><li><a href="global.html#getJsDir">getJsDir</a></li><li><a href="global.html#getLocation">getLocation</a></li><li><a href="global.html#getNetType">getNetType</a></li><li><a href="global.html#getPageHeight">getPageHeight</a></li><li><a href="global.html#getPageViewWidth">getPageViewWidth</a></li><li><a href="global.html#getPageWidth">getPageWidth</a></li><li><a href="global.html#getRandomItem">getRandomItem</a></li><li><a href="global.html#getRequestParams">getRequestParams</a></li><li><a href="global.html#getRoundNum">getRoundNum</a></li><li><a href="global.html#getSearchParams">getSearchParams</a></li><li><a href="global.html#getStringLength">getStringLength</a></li><li><a href="global.html#getSystem">getSystem</a></li><li><a href="global.html#getType">getType</a></li><li><a href="global.html#getUrlState">getUrlState</a></li><li><a href="global.html#hexMd5">hexMd5</a></li><li><a href="global.html#hexToRgba">hexToRgba</a></li><li><a href="global.html#initRequestAnimationFrame">initRequestAnimationFrame</a></li><li><a href="global.html#isContains">isContains</a></li><li><a href="global.html#isDigit">isDigit</a></li><li><a href="global.html#isEndsWith">isEndsWith</a></li><li><a href="global.html#isStartsWith">isStartsWith</a></li><li><a href="global.html#loadScript">loadScript</a></li><li><a href="global.html#loadStyle">loadStyle</a></li><li><a href="global.html#localStorageTest">localStorageTest</a></li><li><a href="global.html#move">move</a></li><li><a href="global.html#numberUpperFormat">numberUpperFormat</a></li><li><a href="global.html#numOfComma">numOfComma</a></li><li><a href="global.html#numOfComma2">numOfComma2</a></li><li><a href="global.html#objToQuery">objToQuery</a></li><li><a href="global.html#pad">pad</a></li><li><a href="global.html#pseudoHack">pseudoHack</a></li><li><a href="global.html#regularExpressionTest">regularExpressionTest</a></li><li><a href="global.html#remInit">remInit</a></li><li><a href="global.html#repeat">repeat</a></li><li><a href="global.html#replaceAll">replaceAll</a></li><li><a href="global.html#reverse">reverse</a></li><li><a href="global.html#sessionStorageTest">sessionStorageTest</a></li><li><a href="global.html#setCookie">setCookie</a></li><li><a href="global.html#showDomOutline">showDomOutline</a></li><li><a href="global.html#stringToDate">stringToDate</a></li><li><a href="global.html#stripHTMLTags">stripHTMLTags</a></li><li><a href="global.html#stripScripts">stripScripts</a></li><li><a href="global.html#SVGTest">SVGTest</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#timeAgo">timeAgo</a></li><li><a href="global.html#toAscii">toAscii</a></li><li><a href="global.html#toHump">toHump</a></li><li><a href="global.html#trim">trim</a></li><li><a href="global.html#truncate">truncate</a></li><li><a href="global.html#ucfirst">ucfirst</a></li><li><a href="global.html#webpAnimataionSupportDetect">webpAnimataionSupportDetect</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> on Mon Feb 14 2022 16:57:07 GMT+0800 (China Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>