-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbignumber.js
137 lines (116 loc) · 3.48 KB
/
bignumber.js
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
'use strict';
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('angular'));
} else {
global.ngBignumber = factory(global.angular);
}
}(this, function(angular) {
bignumberFilter.$inject = ['bignumber'];
return angular.module('ngBignumber', [])
.provider('bignumber', bignumberProvider)
.filter('bignumber', bignumberFilter)
.name;
function bignumberProvider() {
// Constant to hold different languages symbols for respective powers.
var NUMBER_SUFFICES = {
en: [{
pow: 3,
val: 'K',
}, {
pow: 6,
val: 'M',
}, {
pow: 9,
val: 'B',
}],
ja: [{
pow: 3,
val: '千',
}, {
pow: 4,
val: '万',
}, {
pow: 8,
val: '億',
}],
ru: [{
pow: 3,
val: 'тыс',
}, {
pow: 6,
val: 'млн',
}, {
pow: 9,
val: 'млрд',
}],
};
var $prefferedLanguage = 'en';
var $fractionSize = 2;
this.preferredLanguage = preferredLanguage;
this.fractionSize = fractionSize;
this.$get = $get;
function preferredLanguage(langKey) {
$prefferedLanguage = (langKey && NUMBER_SUFFICES[langKey]) ? langKey : 'en';
}
function fractionSize(fSize) {
$fractionSize = fSize;
}
function $get() {
return function bignumberService(number, fractionSize, lang) { // jshint ignore:line
number = Number(number);
if (!number) {
return number;
}
if (!fractionSize && fractionSize !== 0 || fractionSize < 0) {
fractionSize = $fractionSize;
}
if (!lang || !NUMBER_SUFFICES[lang]) {
lang = $prefferedLanguage;
}
//constants for absolute, negativity, index of number
var abs = Math.abs(number);
var isNegative = number < 0;
var langDef = NUMBER_SUFFICES[lang];
var pow = Math.log(abs) / Math.log(10);
if (pow >= 3) {
// Map power to possible smallest mapping symbol for that language.
for (var i = 0, len = langDef.length; i < len; i++) {
var j = i + 1;
var langDefI = langDef[i];
var langDefJ = langDef[j];
var symbol;
var fractionAbs;
// IF NOT the last item in object
if (langDefJ) {
if (pow.toFixed(2) == langDefJ.pow) {
symbol = langDefJ.val;
fractionAbs = (abs / Math.pow(10, langDefJ.pow)).toFixed(fractionSize);
return (isNegative ? '-' : '') + fractionAbs + symbol;
} else if (pow < langDefJ.pow) {
symbol = langDefI.val;
fractionAbs = (abs / Math.pow(10, langDefI.pow)).toFixed(fractionSize);
return (isNegative ? '-' : '') + fractionAbs + symbol;
}
}
// IF last item in object
else {
symbol = langDefI.val;
fractionAbs = (abs / Math.pow(10, langDefI.pow)).toFixed(fractionSize);
return (isNegative ? '-' : '') + fractionAbs + symbol;
}
}
}
return number;
};
}
}
// filter
function bignumberFilter(bignumber) {
return function(number, fractionSize, lang) {
return bignumber(number, fractionSize, lang);
};
}
}));