简体   繁体   English

为浮点数反应输入 type="number"

[英]React input type="number" for floats

I want to have an input field of type="number" .我想要一个type="number"的输入字段。 (So that on mobile phones the numeric keyboard will appear). (这样在手机上就会出现数字键盘)。 The problem is, that all my values are in cents: 1EUR = 100 Cents and I want to display a comma as a decimal separator (German format), so onChange will multiply the value with 100, and when the value is rendered, it is devided by 100. But when I type "5."问题是,我所有的值都以美分为单位:1EUR = 100 Cents,我想显示一个逗号作为小数点分隔符(德语格式),所以 onChange 会将值乘以 100,当该值呈现时,它是除以 100。但是当我输入“5”时。 after typing the next number the "."输入下一个数字后,“。” will get lost.会迷路。 The same happens with a ",". “,”也是如此。

I couldn't find any component already implemented that does this by using input type="number" instead of type="text".我找不到任何已经通过使用 input type="number" 而不是 type="text" 来实现的组件。 So does anyone know such a library or a way to implement it, that does not include having two inputs, one for Cents and one for Euros?那么有没有人知道这样的库或实现它的方法,其中不包括两个输入,一个用于美分,一个用于欧元?

I think the number input type can handle the German comma system.我认为数字输入类型可以处理德语逗号系统。 So there might not be a need to manually change a 2,5 input into a 2.5.因此可能不需要手动将 2,5 输入更改为 2.5。 If you use this changeHandler on a number input如果您在数字输入上使用此 changeHandler

const changeHandler = e => {
    const val = e.target.value
    console.log(2 * val)
    setNumber(val)
  }

the val variable will be a usual float while you see a comma in the input.当您在输入中看到逗号时, val变量将是一个通常的浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM