-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
30 lines (24 loc) · 993 Bytes
/
utils.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
function roundNumber(number, price) {
const getDecimalPlaces = (numero) => {
const parts = numero.toString().split('.');
return parts.length > 1 ? parts[1].length : 0;
};
const roundNumberInternal = (numero, minimamlDecimalPlaces) => {
const multiplicationFactor = Math.pow(10, minimamlDecimalPlaces);
const roundNumberCalc = (Math.round(numero * multiplicationFactor) / multiplicationFactor).toString();
const parts = roundNumberCalc.split('.');
// Adiciona zeros à direita, se necessário
if (parts.length > 1) {
parts[1] = parts[1].padEnd(minimamlDecimalPlaces, '0');
} else if (minimamlDecimalPlaces > 0) {
parts.push('0'.repeat(minimamlDecimalPlaces));
}
return parts.join('.');
};
const decimalPlaces = getDecimalPlaces(price);
const priceRound = roundNumberInternal(number, decimalPlaces);
return priceRound;
}
module.exports = {
roundNumber
}