英文:
Display html-Input-value as german formatted Number on input-change, while keeping track of decimal number
问题
German decimal numbers use a decimal comma. When formatting, we also use the period for grouping digits. So the number 10000.45 would be formatted as 10.000,45.
Now I would like to have an input-field (numeric or text) where you can type the "german" way using periods and commas. However, I want to also keep track of a "normal" numeric value in JavaScript.
Converting a numeric value to a German-formatted value is easy with
number.toLocaleString("de-DE", { maxFractionDigits: 2 })
But how do I get this backformatted to a decimal number? Because when I display the German-formatted value like "7,20," I append to the end, right?
Here is a Svelte REPL with an initial try that looks like this:
https://svelte.dev/repl/fabd0a80f4ee49509cd875c0175bcf22?version=4.0.1
<script>
let numericValue = 0;
let formattedValue = '';
function formatNumber(value) {
return value.toLocaleString("de", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
}
function handleInput(event) {
// Remove dots as thousand separators and convert decimal comma to dot
const inputValue = event.target.value.replace(/\./g, '').replace(/,/g, '.');
// Update numericValue with parsed float
numericValue = parseFloat(inputValue);
// Update formattedValue for display
formattedValue = formatNumber(numericValue);
}
</script>
<input type="text" value={formattedValue} on:input={handleInput} />
<p>Numeric value: {numericValue}</p>
Ideally, I would like to have the displayed, formatted value on the input-event. But it can also be on the on-change event. Or even using a button next to it or something. I'd be grateful for any tip!
英文:
So German decimal numbers use a decimal comma. When formatting we also use the period for grouping digits. So the number 10000.45 would be formatted as 10.000,45.
Now I would like to have an input-field (numeric or text) where you can type the "german" way using periods and commas. However, I want to also keep track of a "normal" numeric value in JavaScript.
Converting a numeric value to a german-formatted value is easy with
number.toLocaleString("de-DE", { maxFractionDigits: 2 })
But how do I get this backformatted to a decimal number? Because when I display the german-formatted value like "7,20" I append to the end right?
Here is a svelte-repl with an initial try that looks like this:
https://svelte.dev/repl/fabd0a80f4ee49509cd875c0175bcf22?version=4.0.1
<script>
let numericValue = 0;
let formattedValue = '';
function formatNumber(value) {
return value.toLocaleString("de", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
}
function handleInput(event) {
// Remove dots as thousand separators and convert decimal comma to dot
const inputValue = event.target.value.replace(/\./g, '').replace(/,/g, '.');
// Update numericValue with parsed float
numericValue = parseFloat(inputValue);
// Update formattedValue for display
formattedValue = formatNumber(numericValue);
}
</script>
<input type="text" value={formattedValue} on:input={handleInput} />
<p>Numeric value: {numericValue}</p>
Ideally, I would like to have the displayed, formatted value on the input-event. But it can also be on the on-change event. Or even using a button next to it or something.
I'd be grateful for any tip!:)
答案1
得分: 1
我建议不要干预用户输入,即不要在input
事件处理程序中设置formattedValue
。如果您想要将数字格式化得漂亮,可以在组件首次显示时和在blur
事件上进行格式化,以防止干扰用户的意图。
如果您真的想要在用户输入时严格规定特定的格式,这是不容易的,因为必须考虑光标位置、数字符号、清除值以及其他因素。也有一些库可以实现这个功能,例如imask
。
英文:
I would recommend just not messing with the user input, i.e. not setting formattedValue
in the input
event handler. If you want to format the number nicely, do it the first time the component is shown and on blur
to prevent interfering with the user's intentions.
If you really want to strictly prescribe a certain format while the user is typing that is non trivial because the cursor position, number sign, clearing the value and other things have to taken into account. There are also libraries for that, e.g. imask
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论