woocommerce/packages/js/number/zh-cn/README.md
2026-04-15 10:18:01 +08:00

43 lines
No EOL
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 数字
一组用于在 WooCommerce 中正确本地化数值的实用工具
## 安装
安装模块
```bash
pnpm install @woocommerce/number --save
```
_此包假定您的代码将在 **ES2015+** 环境中运行。如果您使用的环境对 ES2015+ 支持有限或不支持(例如较低版本的 IE那么使用 [core-js](https://github.com/zloirock/core-js) 或 [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) 将为这些方法添加支持。在 [Babel 文档](https://babeljs.io/docs/en/next/caveats) 中了解更多信息。_
## 用法
```JS
import { numberFormat, formatValue, calculateDelta } from '@woocommerce/number';
// 最好获取站点货币设置,并将其与格式化函数组合使用。
import { partial } from 'lodash';
// 从 API 或全局设置对象中获取此配置。
const siteNumberOptions = {
precision: 2,
decimalSeparator: '.',
thousandSeparator: ',',
};
// 组合。
const formatStoreNumber = partial( numberFormat, siteNumberOptions );
const formatStoreValue = partial( formatValue, siteNumberOptions );
// 使用站点当前区域设置格式化数字。
const localizedNumber = formatStoreNumber( 1337 ); // '1,377'
// formatValue 的第二个参数是类型average 或 number
// 第三个参数是要格式化的数字/值
// (第一个参数是我们之前组合的配置对象)
const formattedAverage = formatStoreValue( 'average', '10.5' ); // 11 仅使用 Math.round
const formattedNumber = formatStoreValue( 'number', '1337' ); // 1,337 调用 formatNumber见上文
// 获取两个数字之间的四舍五入百分比变化/差值
const delta = calculateDelta( 10, 8 ); // '25'
```