Files
radicall/resources/js/Client/utils/numberUtils.js
T

46 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const convertPersianToEnglish = (text) => {
if (text === null || text === undefined) {
return '0'
}
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
let result = String(text)
persianNumbers.forEach((persian, index) => {
result = result.replace(new RegExp(persian, 'g'), englishNumbers[index])
})
return result
}
export const convertEnglishToPersian = (text) => {
if (text === null || text === undefined) {
return '۰'
}
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
let result = String(text)
englishNumbers.forEach((english, index) => {
result = result.replace(new RegExp(english, 'g'), persianNumbers[index])
})
return result
}
export const formatPrice = (price) => {
if (price === null || price === undefined) {
return '۰'
}
const numPrice = Number(price)
if (isNaN(numPrice)) {
return '۰'
}
const formattedPrice = numPrice.toLocaleString('fa-IR')
return convertEnglishToPersian(formattedPrice)
}