number — Number Conversion
17:43Description
$u.getPxNum converts a number, px, % or rpx value into a px number, while $u.toNumber safely converts a number, string or null into a number, returning 0 for invalid input.
Examples
Size conversion
getPxNum handles numbers, px, % and rpx; percentages depend on the total width passed as the second argument, and the rpx result is shown to two decimals.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in pxList"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type Row = {
desc: string;
result: string;
last: boolean;
};
// 结果统一转字符串,避免模板里数字与字符串混排
const pxList: Row[] = [
// 数字入参在 config.unit 为 px 时原样返回
{ desc: '$u.getPxNum(10)', result: $u.getPxNum(10).toString(), last: false },
{ desc: "$u.getPxNum('20px')", result: $u.getPxNum('20px').toString(), last: false },
// 百分比按第二参数的总宽度换算
{ desc: "$u.getPxNum('50%', 200)", result: $u.getPxNum('50%', 200).toString(), last: false },
{ desc: "$u.getPxNum('25%', 750)", result: $u.getPxNum('25%', 750).toString(), last: false },
// rpx 走 uni.rpx2px,随屏宽变化
{ desc: "$u.getPxNum('20rpx')", result: $u.getPxNum('20rpx').toFixed(2), last: true }
];
</script>To number
toNumber follows parseFloat semantics: '6px' yields 6, while non-numeric text, an empty string and null all return 0.
View source
vue
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in toNumList"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
/>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type Row = {
desc: string;
result: string;
last: boolean;
};
// parseFloat 语义:截取前缀数字,非法值与 null 一律返回 0
const toNumList: Row[] = [
{ desc: "$u.toNumber('123')", result: $u.toNumber('123').toString(), last: false },
{ desc: "$u.toNumber('6px')", result: $u.toNumber('6px').toString(), last: false },
{ desc: '$u.toNumber(12.5)', result: $u.toNumber(12.5).toString(), last: false },
{ desc: "$u.toNumber('abc')", result: $u.toNumber('abc').toString(), last: false },
{ desc: "$u.toNumber('')", result: $u.toNumber('').toString(), last: false },
{ desc: '$u.toNumber(null)', result: $u.toNumber(null).toString(), last: true }
];
</script>API
getPxNum
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | A number, or a px / % / rpx string | string | number | - | - |
| totalWidth | Total width in px when resolving a percentage | number | 0 | - |
Returns:
| Type | Description |
|---|---|
number | The resolved px value |
toNumber
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to convert | number | string | null | - | - |
Returns:
| Type | Description |
|---|---|
number | The parsed number; null, an empty string and invalid values return 0 |
Notes
- A numeric input to
getPxNumis returned as-is unlessconfig.unitisrpx, in which case it is converted through rpx first. %is resolved againsttotalWidth, which defaults to0, so a percentage resolves to 0 when the second argument is omitted.rpxgoes throughuni.rpx2px, so results vary with screen width and are not consistent across devices.toNumberfollowsparseFloatsemantics and only reads the numeric prefix; it never returnsNaN.