This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
number-formatting.js
109 lines (92 loc) · 3.61 KB
/
number-formatting.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
/**
* @module saxo/number-formatting/number-formatting
* @ignore
*/
import formatNumber from './format';
import parseNumber from './parse';
import shortFormat from './short-format';
import { extend } from '../utils/object';
// -- Local variables section --
const defaultOptions = {
groupSizes: [3],
groupSeparator: ',',
decimalSeparator: '.',
negativePattern: '-{0}',
};
const numberOfZerosRx = /0+$/;
// -- Local methods section --
// -- Exported methods section --
/**
* A class which does number formatting and parsing.
* @class
* @alias saxo.NumberFormatting
* @param {Object} [options] - Number locale options.
* @param {Array.<number>} [options.groupSizes=[3]] - The group sizes for the number.
* E.g. [3] would be thousands seperator and produce 123.456.789,00 where as [2,3] would be "12.34.56.789,00".
* @param {string} [options.groupSeparator=","] - The character used for group separation E.g. '.' in Danish.
* @param {string} [options.decimalSeparator="."] - The character used for decimal searation E.g.',' in Danish.
* @param {string} [options.negativePattern="-{0}"] - The negative pattern to use with '{0}' as the placeholder for the non-negative number.
*/
function NumberFormatting(options) {
extend(this, defaultOptions, options);
this.negativePre = this.negativePattern.substr(0, this.negativePattern.indexOf('{'));
this.negativePost = this.negativePattern.substr(this.negativePattern.indexOf('}') + 1);
}
/**
* Parses a localised string into a number.
* @param {string} value - The number to parse.
* @returns {number} parsed value
*/
NumberFormatting.prototype.parse = function(value) {
return parseNumber(value, this);
};
/**
* Formats a number into a localised string.
* @param {number} num - The number to format.
* @param {number} [decimals] - The number of decimals to display after the decimal point.
* If undefined then the number is formatted with however many decimal places it needs to display the number (upto 8).
* @returns {string}
*/
NumberFormatting.prototype.format = function(num, decimals) {
if (decimals === undefined || decimals === null) {
decimals = this.getActualDecimals(num);
}
return formatNumber(num, decimals, this);
};
/**
* Formats the number without rounding. e.g. 1.12 formatted with 1 decimal place is "1.12".
* @param {number} num - The number to format
* @param {number} [minDecimals] - The minimum number of decimals to display after the decimal point.
* @param {number} [maxDecimals] - The maximum number of decimals to display after the decimal point.
* @returns {string}
*/
NumberFormatting.prototype.formatNoRounding = function(num, minDecimals, maxDecimals) {
if (!minDecimals) {
minDecimals = 0;
}
if (!maxDecimals) {
maxDecimals = 8;
}
return formatNumber(num,
Math.min(maxDecimals, Math.max(minDecimals, this.getActualDecimals(num))),
this);
};
/**
* Formats a number into a short format, e.g. 10.000 becomes 10k.
* @param {number} number
* @returns {string}
*/
NumberFormatting.prototype.shortFormat = function(number) {
return shortFormat(number, this);
};
/**
* Returns the actual number of decimals that a number has.
* @param number
* @returns {number}
*/
NumberFormatting.prototype.getActualDecimals = function(number) {
number = Math.abs(number);
return (number - Math.floor(number)).toFixed(8).substring(2, 10).replace(numberOfZerosRx, '').length;
};
// -- Export section --
export default NumberFormatting;