【发布时间】:2020-10-05 09:47:09
【问题描述】:
我正在构建一个响应表单,其中包含一些需要用货币格式化的输入。所以需要以$为前缀,加上逗号、小数(2个小数点)。
我找到了这个名为 dinerojs.com: https://dinerojs.com/ 的库。
我不确定我做错了什么,但这不是格式化。该字段的工作方式与现在一样,但没有格式。有什么想法吗?
//State
ProductPrice1: number,
//Set inital state
ProductPrice1: Dinero({ amount: 0 }).toFormat('$0,0.00'),
//handle bind
this.handleChangeProductPrice1 = this.handleChangeProductPrice1.bind(this);
//Change function
handleChangeProductPrice1y = async (event: any) => {
const value = parseFloat(event.target.value)
await this.setState({ ProductPrice1: value });
}
//in my render
<input
value={this.state.ProductPrice1}
onChange={this.handleChangeProductPrice1}
type="number"
className="phone validate"
name="ProductPrice1"
/>
【问题讨论】:
-
你不应该在
handleChangeProductPrice1y函数中重新格式化数字吗?设置状态时也不需要async和await -
@ludwiguer 你能把这个作为答案发布,如果它有效,我可以将它设置为正确答案
标签: javascript reactjs typescript forms