我无法将输入值转换为货币格式。我想在用户输入数字(5,000.00; 125,000.00)时自动添加千位和小数分隔符。
这是我的代码:
$('input.CurrencyInput').on('blur, focus, keyup',
function() {
$(this).val().toLocaleString('en-US', {
style: 'decimal',
maximumFractionDigits: 2,
minimumFractionDigits: 2
});
});
答案 0 :(得分:6)
您的代码存在一些问题:
toLocaleString
之前,您未将收到的值转换为数字。纠正这些,这是一个工作演示。为简单起见,我删除了除blur
之外的其他事件处理程序,因为keyup
导致了问题。
$('input.CurrencyInput').on('blur', function() {
const value = this.value.replace(/,/g, '');
this.value = parseFloat(value).toLocaleString('en-US', {
style: 'decimal',
maximumFractionDigits: 2,
minimumFractionDigits: 2
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="CurrencyInput">
答案 1 :(得分:1)
以下函数将尝试将任何乱码输入值解析为特定货币。
如果您不想强制用户输入特定语言环境格式的值,则很有用。
警告:
由于输入实际上可能像 1,2.3.45,5
一样乱码,但仍然提供有效输出(例如美元:"12,345.50"
),因此有一个小但用户友好的警告:< /p>
// Example: conversion to HRK (Format: n.nnn,nn)
INPUT: "0.575" OUTPUT: "0,58" // smart guessed: decimals
INPUT: "1.575" OUTPUT: "1.575,00" // smart guessed: separator
示例:
/**
* Parse value to currency
* @param {number|string} input
* @param {string} locale - Desired locale i.e: "en-US" "hr-HR"
* @param {string} currency - Currency to use "USD" "EUR" "HRK"
* @return {object}
*/
const parse = (input, locale = "en-US", currency = "USD") => {
let fmt = String(input)
.replace(/(?<=\d)[.,](?!\d+$)/g, "")
.replace(",", ".");
const pts = fmt.split(".");
if (pts.length > 1) {
if (+pts[0] === 0) fmt = pts.join(".");
else if (pts[1].length === 3) fmt = pts.join("");
}
const number = Number(fmt);
const isValid = isFinite(number);
const string = number.toFixed(2);
const intlNFOpts = new Intl.NumberFormat(locale, {
style: "currency",
currency: currency,
}).resolvedOptions();
const output = number.toLocaleString(locale, {
...intlNFOpts,
style: "decimal",
});
return {
input,
isValid,
string,
number,
currency,
output,
};
};
/**
* Parse value to currency
* @param {number|string} input
* @param {string} locale - Desired locale i.e: "en-US" "hr-HR"
* @param {string} currency - Currency to use "USD" "EUR" "HRK"
* @return {object}
*/
const parse = (input, locale = "en-US", currency = "USD") => {
let fmt = String(input)
.replace(/(?<=\d)[.,](?!\d+$)/g, "")
.replace(",", ".");
const pts = fmt.split(".");
if (pts.length > 1) {
if (+pts[0] === 0) fmt = pts.join(".");
else if (pts[1].length === 3) fmt = pts.join("");
}
const number = Number(fmt);
const isValid = isFinite(number);
const string = number.toFixed(2);
const intlNFOpts = new Intl.NumberFormat(locale, {
style: "currency",
currency: currency,
}).resolvedOptions();
const output = number.toLocaleString(locale, {
...intlNFOpts,
style: "decimal",
});
return {
input,
isValid,
string,
number,
currency,
output,
};
};
// TEST:
[
// Valid values
"2e5",
"1,2.3,10,6",
"0.575",
"1.575",
"2.30",
"1.000.00",
"1.000",
1000,
1,
".4",
"4.",
"0.25",
"0.076",
"1.076",
0.3478,
"0.05",
"123,123",
"3.000,333.444,009",
"123,123.80",
"23.123,80",
"23.123,8",
"23.4",
// Invalid values
null,
NaN,
Infinity,
"a",
].forEach((val) => {
const p = parse(val, "hr-HR", "HRK");
console.log(`INP: ${p.input}\t OUT: ${p.output}`);
});
.as-console-wrapper {min-height: 100vh;}
文档: