Last Updated on : 2024-06-05 03:14:36download
This topic describes the utility methods related to numbers.
toFixedString
Extract a substring with the required length that starts from the end of a specified string. If the string length is less than the required one, one or more leading zeros are added to the string to reach the required length.
Name | Data type | Description | Required |
---|---|---|---|
value | number | The value to be converted. | Yes |
length | number | The length of the returned string. | Yes |
Name | Data type | Description |
---|---|---|
result | string | The returned string. |
import { Utils } from "tuya-panel-kit";
const { toFixedString } = Utils.NumberUtils;
toFixedString(value, length);
// Example:
toFixedString(111, 5);
toFixedString(3456111, 5);
'00111'
'56111'
toFilledString
If the length of a specified string is less than the required length, pad the string with one or more leading zeros to the required length. If the string length is more than the required one, return the string.
Name | Data type | Description | Required |
---|---|---|---|
value | number | The value to be converted. | Yes |
length | number | The length of the returned string. | Yes |
Name | Data type | Description |
---|---|---|
result | string | The returned string. |
import { Utils } from "tuya-panel-kit";
const { toFilledString } = Utils.NumberUtils;
toFilledString(value, length);
// Example:
toFixedString(111, 5);
toFixedString(3456111, 5);
'00111'
'3456111'
bytesToHexString
Convert an 8-bit integer array to a hexadecimal string. High-order bits are separated from low-order bits in the conversion result.
Name | Data type | Description | Required |
---|---|---|---|
array | number | The array in which each number is an eight-bit integer. | Yes |
Name | Data type | Description |
---|---|---|
result | string | The returned string. |
import { Utils } from "tuya-panel-kit";
const { bytesToHexString } = Utils.NumberUtils;
bytesToHexString(array);
// example:
bytesToHexString([1, 2]);
bytesToHexString([23, 2]);
'0102'
'1702'
numToByteNumbers
Convert a specified decimal string to a hexadecimal array.
Name | Data type | Description | Required |
---|---|---|---|
value | number | The value to be converted. | Yes |
Name | Data type | Description |
---|---|---|
result | string | The returned string in which each character is hexadecimal. |
import { Utils } from "tuya-panel-kit";
const { numToByteNumbers } = Utils.NumberUtils;
numToByteNumbers(value);
// Example:
numToHexString(111);
numToHexString(15);
'6f'
'0f'
intToHighLow
Convert a specified decimal string to an 8-bit integer array.
Name | Data type | Description | Required |
---|---|---|---|
value | number | The value to be converted. | Yes |
Name | Data type | Description |
---|---|---|
result | number | An array that is converted from a decimal string. The first element of the array indicates the high-order eight bits and the second element indicates the low-order eight bits. |
import { Utils } from "tuya-panel-kit";
const { intToHighLow } = Utils.NumberUtils;
intToHighLow(value);
// Example:
intToHighLow(2838);
intToHighLow(5643);
[11, 22]
[22, 11]
inMaxMin
Get the median of the specified three numbers.
Name | Data type | Description | Required |
---|---|---|---|
min | number | The minimum value. | Yes |
max | number | The maximum value. | Yes |
value | number | The specific value. | Yes |
Name | Data type | Description |
---|---|---|
result | number | The median of the specified three numbers. |
import { Utils } from "tuya-panel-kit";
const { inMaxMin } = Utils.NumberUtils;
inMaxMin(min, max, value);
// Example:
inMaxMin(1, 2838, 233);
inMaxMin(1, 2, 0);
233
1
scaleNumber
Divide a value by 10 to the power of num
.
Name | Data type | Description | Required |
---|---|---|---|
num | number | The exponent of 10. | Yes |
value | number | The original value. | Yes |
Name | Data type | Description |
---|---|---|
result | number | value / Math.pow(10, scale) |
import { Utils } from "tuya-panel-kit";
const { scaleNumber } = Utils.NumberUtils;
scaleNumber(num, value);
// Example:
scaleNumber(2, 10245);
102.45
range
Get an array of numbers from a start value to an end value with the specified step size.
Name | Data type | Description | Required |
---|---|---|---|
start | number | The start value. | Yes |
end | number | The end value. | Yes |
value | number | The step size. | Yes |
Name | Data type | Description |
---|---|---|
result | number | The array of numbers from the start value to the end value with the specified step size. |
import { Utils } from "tuya-panel-kit";
const { range } = Utils.NumberUtils;
range(start, end, value);
// Example:
range(1, 10);
range(1, 10, 2);
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]
calcPosition
Convert the value in the original range to the value in the new range.
Name | Data type | Description | Required |
---|---|---|---|
value | number | The original value. | Yes |
min | number | The minimum value of the original range. | Yes |
max | number | The maximum value of the original range. | Yes |
newMin | number | The minimum value of the new range. | Yes |
newMax | number | The maximum value of the new range. | Yes |
Name | Data type | Description |
---|---|---|
result | number | ((value - min) * newRange) / oldRange + newMin |
import { Utils } from "tuya-panel-kit";
const { calcPosition } = Utils.NumberUtils;
calcPosition(value, min, max, newMin, newMax);
// Example:
calcPosition(50, 0, 100, -100, 0);
calcPosition(255, 0, 255, 0, 100);
-50
100
calcPercent
Calculate the offset percentage in the new range.
Name | Data type | Description | Required |
---|---|---|---|
min | number | The minimum value. | Yes |
max | number | The maximum value. | Yes |
value | number | The specific value. | Yes |
offset | number | The offset percentage. Valid values: 0 to 1 . |
No |
Name | Data type | Description |
---|---|---|
result | number | The offset percentage in the new range. |
import { Utils } from "tuya-panel-kit";
const { calcPercent } = Utils.NumberUtils;
calcPercent(min, max, value, offset);
// Example:
calcPercent(25, 255, 25);
calcPercent(25, 255, 25, 0.1);
0
0.1
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback