mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-20 12:28:10 +08:00
fdb943e887
The replace function can only work on a string, this little tweak will make the formatter not crash the page when the value is changed.
878 B
878 B
order | title | ||||
---|---|---|---|---|---|
4 |
|
zh-CN
通过 formatter
格式化数字,以展示具有具体含义的数据,往往需要配合 parser
一起使用。
en-US
Display value within it's situation with formatter
, and we usually use parser
at the same time.
import { InputNumber } from 'antd';
function onChange(value) {
console.log('changed', value);
}
ReactDOM.render(
<div>
<InputNumber
defaultValue={1000}
formatter={value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
parser={value => value.replace(/\$\s?|(,*)/g, '')}
onChange={onChange}
/>
<InputNumber
defaultValue={100}
min={0}
max={100}
formatter={value => `${value}%`}
parser={value => value.replace('%', '')}
onChange={onChange}
/>
</div>
, mountNode);