solve bug of numbers
This commit is contained in:
@@ -397,16 +397,69 @@ const form = ref<Gold>({
|
|||||||
})
|
})
|
||||||
const allowedAsciiCodeFloat = [46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
|
const allowedAsciiCodeFloat = [46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
|
||||||
const allowedAsciiCodeInt = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
|
const allowedAsciiCodeInt = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
|
||||||
|
|
||||||
|
// Function to convert Persian/Farsi numbers to English
|
||||||
|
function convertPersianToEnglish(str: string): string {
|
||||||
|
const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
|
||||||
|
const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||||||
|
const persianDecimal = '٫'
|
||||||
|
const englishDecimal = '.'
|
||||||
|
|
||||||
|
let result = str
|
||||||
|
// Replace Persian numbers with English numbers
|
||||||
|
for (let i = 0; i < persianNumbers.length; i++) {
|
||||||
|
result = result.replace(new RegExp(persianNumbers[i], 'g'), englishNumbers[i])
|
||||||
|
}
|
||||||
|
// Replace Persian decimal separator with English decimal separator
|
||||||
|
result = result.replace(new RegExp(persianDecimal, 'g'), englishDecimal)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler for Persian number input
|
||||||
|
function handlePersianNumberInput(event: Event) {
|
||||||
|
const target = event.target as HTMLInputElement
|
||||||
|
if (target && target.value) {
|
||||||
|
const convertedValue = convertPersianToEnglish(target.value)
|
||||||
|
if (convertedValue !== target.value) {
|
||||||
|
form.value.goldValue = convertedValue as any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
watch(form.value, () => {
|
watch(form.value, () => {
|
||||||
if (form.value.goldValue) {
|
if (form.value.goldValue) {
|
||||||
const lastCharGoldStr: string = form.value.goldValue.toString()
|
const goldValueStr = form.value.goldValue.toString()
|
||||||
|
// Convert Persian numbers to English
|
||||||
|
const convertedValue = convertPersianToEnglish(goldValueStr)
|
||||||
|
if (convertedValue !== goldValueStr) {
|
||||||
|
const numericValue = parseFloat(convertedValue)
|
||||||
|
form.value.goldValue = isNaN(numericValue) ? null : numericValue
|
||||||
|
if (!isNaN(numericValue)) {
|
||||||
|
calculateCash(numericValue)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastCharGoldStr: string = goldValueStr
|
||||||
if (!allowedAsciiCodeFloat.includes(lastCharGoldStr.slice(-1).charCodeAt(0))) {
|
if (!allowedAsciiCodeFloat.includes(lastCharGoldStr.slice(-1).charCodeAt(0))) {
|
||||||
form.value.goldValue = isNaN(parseFloat(lastCharGoldStr.slice(0, -1))) ? null : parseFloat(lastCharGoldStr.slice(0, -1))
|
form.value.goldValue = isNaN(parseFloat(lastCharGoldStr.slice(0, -1))) ? null : parseFloat(lastCharGoldStr.slice(0, -1))
|
||||||
calculateCash(form.value.goldValue)
|
calculateCash(form.value.goldValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (form.value.goldCash) {
|
if (form.value.goldCash) {
|
||||||
const lastCharCashStr: string = form.value.goldCash.toString()
|
const goldCashStr = form.value.goldCash.toString()
|
||||||
|
// Convert Persian numbers to English
|
||||||
|
const convertedValue = convertPersianToEnglish(goldCashStr)
|
||||||
|
if (convertedValue !== goldCashStr) {
|
||||||
|
const numericValue = parseFloat(convertedValue.replaceAll(',', ''))
|
||||||
|
form.value.goldCash = isNaN(numericValue) ? null : numericValue
|
||||||
|
if (!isNaN(numericValue)) {
|
||||||
|
calculateGold(numericValue)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastCharCashStr: string = goldCashStr
|
||||||
if (!allowedAsciiCodeInt.includes(lastCharCashStr.slice(-1).charCodeAt(0))) {
|
if (!allowedAsciiCodeInt.includes(lastCharCashStr.slice(-1).charCodeAt(0))) {
|
||||||
form.value.goldCash = isNaN(parseFloat(lastCharCashStr.slice(0, -1))) ? null : parseFloat(lastCharCashStr.slice(0, -1))
|
form.value.goldCash = isNaN(parseFloat(lastCharCashStr.slice(0, -1))) ? null : parseFloat(lastCharCashStr.slice(0, -1))
|
||||||
calculateGold(form.value.goldCash)
|
calculateGold(form.value.goldCash)
|
||||||
|
|||||||
Reference in New Issue
Block a user