NumberUtils

Last Updated on : 2023-10-12 08:00:25download

This topic describes the utility methods related to numbers.

Get a string with the required length

API operation name

toFixedString

API description

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.

Request parameter

Name Data type Description Required
value number The value to be converted. Yes
length number The length of the returned string. Yes

Return parameter

Name Data type Description
result string The returned string.

Sample request

import { Utils } from "tuya-panel-kit";
const { toFixedString } = Utils.NumberUtils;

toFixedString(value, length);
// Example:
toFixedString(111, 5);
toFixedString(3456111, 5);

Sample response

'00111'
'56111'

Pad or get a specified string

API operation name

toFilledString

API description

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.

Request parameter

Name Data type Description Required
value number The value to be converted. Yes
length number The length of the returned string. Yes

Return parameter

Name Data type Description
result string The returned string.

Sample request

import { Utils } from "tuya-panel-kit";
const { toFilledString } = Utils.NumberUtils;

toFilledString(value, length);
// Example:
toFixedString(111, 5);
toFixedString(3456111, 5);

Sample response

'00111'
'3456111'

Convert an 8-bit integer array to hexadecimal

API operation name

bytesToHexString

API description

Convert an 8-bit integer array to a hexadecimal string. High-order bits are separated from low-order bits in the conversion result.

Request parameter

Name Data type Description Required
array number The array in which each number is an eight-bit integer. Yes

Return parameter

Name Data type Description
result string The returned string.

Sample request

import { Utils } from "tuya-panel-kit";
const { bytesToHexString } = Utils.NumberUtils;

bytesToHexString(array);
// example:
bytesToHexString([1, 2]);
bytesToHexString([23, 2]);

Sample response

'0102'
'1702'

Convert decimal to hexadecimal

API operation name

numToByteNumbers

API description

Convert a specified decimal string to a hexadecimal array.

Request parameter

Name Data type Description Required
value number The value to be converted. Yes

Return parameter

Name Data type Description
result string The returned string in which each character is hexadecimal.

Sample request

import { Utils } from "tuya-panel-kit";
const { numToByteNumbers } = Utils.NumberUtils;

numToByteNumbers(value);
// Example:
numToHexString(111);
numToHexString(15);

Sample response

'6f'
'0f'

Convert decimal to an 8-bit integer array

API operation name

intToHighLow

API description

Convert a specified decimal string to an 8-bit integer array.

Request parameter

Name Data type Description Required
value number The value to be converted. Yes

Return parameter

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.

Sample request

import { Utils } from "tuya-panel-kit";
const { intToHighLow } = Utils.NumberUtils;

intToHighLow(value);

// Example:
intToHighLow(2838);
intToHighLow(5643);

Sample response

[11, 22]
[22, 11]

Get the median of three numbers

API operation name

inMaxMin

API description

Get the median of the specified three numbers.

Request parameter

Name Data type Description Required
min number The minimum value. Yes
max number The maximum value. Yes
value number The specific value. Yes

Return parameter

Name Data type Description
result number The median of the specified three numbers.

Sample request

import { Utils } from "tuya-panel-kit";
const { inMaxMin } = Utils.NumberUtils;

inMaxMin(min, max, value);
// Example:
inMaxMin(1, 2838, 233); 
inMaxMin(1, 2, 0);

Sample response

233
1

Scale down a value

API operation name

scaleNumber

API description

Divide a value by 10 to the power of num.

Request parameter

Name Data type Description Required
num number The exponent of 10. Yes
value number The original value. Yes

Return parameter

Name Data type Description
result number value / Math.pow(10, scale)

Sample request

import { Utils } from "tuya-panel-kit";
const { scaleNumber } = Utils.NumberUtils;

scaleNumber(num, value);
// Example:
scaleNumber(2, 10245);

Sample response

102.45

Get a specified array

API operation name

range

API description

Get an array of numbers from a start value to an end value with the specified step size.

Request parameter

Name Data type Description Required
start number The start value. Yes
end number The end value. Yes
value number The step size. Yes

Return parameter

Name Data type Description
result number The array of numbers from the start value to the end value with the specified step size.

Sample request

import { Utils } from "tuya-panel-kit";
const { range } = Utils.NumberUtils;

range(start, end, value);
// Example:
range(1, 10);
range(1, 10, 2);

Sample response

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]

Convert a value between different ranges

API operation name

calcPosition

API description

Convert the value in the original range to the value in the new range.

Request parameter

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

Return parameter

Name Data type Description
result number ((value - min) * newRange) / oldRange + newMin

Sample request

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);

Sample response

-50
100

Calculate the offset percentage

API operation name

calcPercent

API description

Calculate the offset percentage in the new range.

Request parameter

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

Return parameter

Name Data type Description
result number The offset percentage in the new range.

Sample request

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);

Sample response

0
0.1