@dtinsight/dt-utils
Version:
36 lines (35 loc) • 987 B
TypeScript
/**
* 将数字格式化为百分比字符串。
*
* @category 转换
* @description
* 将十进制数转换为百分比字符串表示。
*
* @param {number} num - 要转换为百分比的十进制数
* @param {number} [precision=2] - 要四舍五入到的小数位数(默认值:2)
* @returns {string} 具有指定精度的格式化百分比字符串
*
* @example
* ```typescript
* import { toPercent } from 'dt-utils';
*
* // 基本用法
* toPercent(0.1234) // => "12.34%"
*
* // 自定义精度
* toPercent(0.1234, 1) // => "12.3%"
* toPercent(1.2345, 3) // => "123.345%"
*
* // 处理负数
* toPercent(-0.089, 1) // => "-8.9%"
*
* // 边界情况
* toPercent(0.00000001) // => "0%"
* toPercent(NaN) // => "0%"
* toPercent(Infinity) // => "0%"
* toPercent(null) // => "0%"
* toPercent(undefined) // => "0%"
* ```
*/
declare const toPercent: (num: number, precision?: number) => string;
export default toPercent;