Created
December 11, 2020 06:39
-
-
Save elexfreeman/da1fc267db1540bc4c9372597b5f33e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* для форматирования валюты | |
* @param n - число | |
* @param r - сколько знаков после запятой | |
*/ | |
export const fNumToStr = (n: number, r?: number): string => { | |
let resp = '0'; | |
if (r) { | |
resp = n.toFixed(Number(r)); | |
} else { | |
resp = n.toFixed(0) | |
} | |
resp = resp.replace('.', ','); | |
// убераем лишние нули в конце | |
let aD = resp.split(','); | |
if (aD.length > 1) { | |
let b = true; | |
while (b) { | |
if (aD[1][aD[1].length - 1] == '0') { | |
aD[1] = aD[1].slice(0, -1); | |
} else { | |
b = false; | |
} | |
} | |
} | |
// раставляем пробелы посл е3-х знаков | |
let aD0 = []; | |
let k = aD[0].length - 1; | |
while (k >= 0) { | |
aD0.push(aD[0][k]); | |
if ((aD[0].length - k) % 3 == 0 && (k != 0)) { | |
aD0.push(' '); | |
} | |
k--; | |
} | |
aD[0] = aD0.reverse().join(''); | |
if (aD.length > 0 && aD[1] != '') { | |
resp = aD.join(','); | |
} else { | |
resp = aD[0]; | |
} | |
return resp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment