2017-03-06 11:40:23 +08:00
---
order: 4
title:
2019-05-07 14:57:32 +08:00
zh-CN: 格式化展示
en-US: Formatter
2017-03-06 11:40:23 +08:00
---
## zh-CN
2017-04-10 11:16:41 +08:00
通过 `formatter` 格式化数字,以展示具有具体含义的数据,往往需要配合 `parser` 一起使用。
2017-03-06 11:40:23 +08:00
2020-08-16 19:32:48 +08:00
> 这里有一个更复杂的货币格式化输入框:[https://codesandbox.io/s/currency-wrapper-antd-input-3ynzo](https://codesandbox.io/s/currency-wrapper-antd-input-3ynzo)
2017-03-06 11:40:23 +08:00
## en-US
2017-04-10 11:16:41 +08:00
Display value within it's situation with `formatter` , and we usually use `parser` at the same time.
2017-03-06 11:40:23 +08:00
2020-08-16 19:32:48 +08:00
> Here is a Intl.NumberFormat InputNumber implementation: [https://codesandbox.io/s/currency-wrapper-antd-input-3ynzo](https://codesandbox.io/s/currency-wrapper-antd-input-3ynzo)
2022-05-19 09:46:26 +08:00
```tsx
2021-09-24 12:25:31 +08:00
import { InputNumber, Space } from 'antd';
2022-05-23 14:37:16 +08:00
import React from 'react';
2017-03-06 11:40:23 +08:00
2022-05-19 09:46:26 +08:00
const onChange = (value: number | string) => {
2017-03-06 11:40:23 +08:00
console.log('changed', value);
2022-05-19 09:46:26 +08:00
};
2017-03-06 11:40:23 +08:00
2022-05-19 09:46:26 +08:00
const App: React.FC = () => (
2021-09-24 12:25:31 +08:00
< Space >
2017-04-10 11:16:41 +08:00
< InputNumber
defaultValue={1000}
2017-08-06 11:57:56 +08:00
formatter={value => `$ ${value}` .replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
2022-05-19 09:46:26 +08:00
parser={value => value!.replace(/\$\s?|(,*)/g, '')}
2017-04-10 11:16:41 +08:00
onChange={onChange}
/>
< InputNumber
defaultValue={100}
min={0}
max={100}
formatter={value => `${value}%` }
2022-05-19 09:46:26 +08:00
parser={value => value!.replace('%', '')}
2017-04-10 11:16:41 +08:00
onChange={onChange}
/>
2022-04-03 23:27:45 +08:00
< / Space >
2018-11-28 15:00:03 +08:00
);
2022-05-19 09:46:26 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```