Created
August 14, 2023 07:14
-
-
Save sglazov/d2da8188bf4017f3acfb9b91b45cdb09 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
/** | |
* Отформатировать цену в рублях с символом ₽ | |
* | |
* Примеры: | |
* * 5000 → 5 000 ₽ | |
* * 712345 → 712 345 ₽ | |
* | |
* @author https://github.com/sglazov | |
* @since 2023-03-16 | |
* | |
* @param {number || string} value Цена | |
* @returns {string} Возвращает отформатированную строку с ценой и символом рубля | |
*/ | |
export function formatPrice(value: number|string) | |
{ | |
let price = '' + value | |
let currency_symbol = '₽' | |
let price_length = price.length | |
let formatted_price = '' | |
while ( price_length > 0 ) | |
{ | |
let start = price_length - 3 > 0 ? price_length - 3 : 0 | |
formatted_price = ' ' + price.substring(start, start+3) + formatted_price | |
price = price.substring(0, start) | |
price_length = price.length | |
} | |
return formatted_price.replace(/^\s+/, '') + ' ' + currency_symbol | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment