Display html-Input-value as german formatted Number on input-change, while keeping track of decimal number

huangapple go评论80阅读模式
英文:

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! Display html-Input-value as german formatted Number on input-change, while keeping track of decimal number

英文:

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(&quot;de-DE&quot;, { 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

&lt;script&gt;
  let numericValue = 0;
  let formattedValue = &#39;&#39;;

  function formatNumber(value) {
    return value.toLocaleString(&quot;de&quot;, {
      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, &#39;&#39;).replace(/,/g, &#39;.&#39;);

    // Update numericValue with parsed float
    numericValue = parseFloat(inputValue);

    // Update formattedValue for display
    formattedValue = formatNumber(numericValue);
  }
&lt;/script&gt;

&lt;input type=&quot;text&quot; value={formattedValue} on:input={handleInput} /&gt;

&lt;p&gt;Numeric value: {numericValue}&lt;/p&gt;

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.

huangapple
  • 本文由 发表于 2023年6月30日 01:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定