Embedded functions

Type conversion

Function

Description

toBoolean(integer) boolean

Converts the argument to a boolean.
Returns true if the argument is 1, false if the argument is 0, or an error otherwise.


Argument: integer value.

toBoolean(string) boolean

Converts the argument to a boolean.
Returns true if the argument is "true", false if the argument is "false", or an error otherwise.


Argument: string value.

toInteger(boolean) integer

Converts the argument to an integer.
Returns 1 if the argument is true and 1 if the argument is false.


Argument: boolean value.

toInteger(number) integer

Converts the argument to an integer. Rounds down the result.
Returns an integer.


Argument: number value.

toInteger(string) integer

Converts the argument to an integer.
Returns an integer.


Argument: string value.

toNumber(integer) number

Converts the argument to a floating-point number.
Returns a number.


Argument: integer value.

toNumber(string) number

Converts the argument to a floating-point number.
Returns a number.


Argument: string value.

toString(boolean) string

Converts the argument to a string.
Returns a string.


Argument: boolean value.

toString(integer) string

Converts the argument to a string.
Returns a string.


Argument: integer value.

toString(number) string

Converts the argument to a string.
Returns a string.


Argument: number value.

encodeUri(string) string

Encodes the string in application/x-www-form-urlencoded format.
Returns the encoded string value.


Argument: string value.

decodeUri(string) string

Decodes the application/x-www-form-urlencoded string.
Returns the decoded string value.


Argument: string value.

Mathematical operations

Mathematical operations

Function

Description

div(integer, integer) integer

Calculates the quotient of the first number divided by the second. Rounds down the result. For example: div(5,2)2.
Returns an integer.


Arguments: 1 — integer dividend. 2 — integer divisor.

div(number, number) number

Calculates the quotient of the first number divided by the second.
Returns a number.


Arguments: 1 — number dividend. 2 — number divisor.

mod(integer, integer) integer

Calculates the remainder of the first number divided by the second.
Returns an integer.


Arguments: 1 — integer dividend. 2 — integer divisor.

mul(vararg integer) integer

Calculates the product of the arguments.
Returns an integer.


Arguments: multiple integer values.

mul(vararg number) number

Calculates the product of the arguments.
Returns a number.


Arguments: Multiple number values.

sub(vararg integer) integer

Calculates the difference of the arguments.
Returns an integer.


Arguments: 1 — integer value to subtract the other arguments from. Other arguments — One or more integer values to be subtracted from Argument 1.

sub(vararg number) number

Calculates the difference of the arguments.
Returns a number.


Arguments: 1 — number value to subtract the other arguments from. Other arguments — One or more number values to be subtracted from Argument 1.

sum(vararg integer) integer

Calculates the sum of the arguments.
Returns an integer.


Arguments: multiple integer values.

sum(vararg number) number

Calculates the sum of the arguments.
Returns a number.


Arguments: Multiple number values.

abs(number) number

Calculates the absolute value of the argument.
Returns a number.


Argument: number value.

abs(integer) integer

Calculates the absolute value of the argument.
Returns an integer.


Argument: integer value.

max(vararg number) number

Finds the argument with the highest value.
Returns a number.


Arguments: Multiple number values.

max(vararg integer) integer

Finds the argument with the highest value.
Returns an integer.


Arguments: multiple integer values.

min(vararg integer) integer

Finds the argument with the lowest value.
Returns an integer.


Arguments: multiple integer values.

min(vararg number) number

Finds the argument with the lowest value.
Returns a number.


Arguments: Multiple number values.

maxNumber() number

Finds the number argument with the highest value.
Returns a number.

No arguments.

maxInteger() integer

Finds the integer argument with the highest value.
Returns an integer.

No arguments.

minNumber() number

Finds the number argument with the lowest value.
Returns a number.

No arguments.

minInteger() integer

Finds the integer argument with the lowest value.
Returns an integer.

No arguments.

round(number) number

Rounds the argument. For example, 1.49 → 1.0, 1.5 → 2.0.
Returns a number.


Argument: number value.

floor(number) number

Rounds the argument down. For example, 1.49 → 1.0, 1.5 → 1.0.
Returns a number.


Argument: number value.

ceil(number) number

Rounds the argument up. For example, 1.49 → 2.0, 1.5 → 2.0.
Returns a number.


Argument: number value.

signum(number) number

Finds the sign of the argument.
Returns -1.0 for negative values, 0.0 for zero, and 1.0 for positive values.


Argument: number value.

signum(integer) integer

Finds the sign of the argument.
Returns -1 for negative values, 0 for zero, and 1 for positive values.


Argument: integer value.

copySign(number, number) number

Gets the number with the magnitude of the first argument and the sign of the second argument.
Returns a number.


Arguments: 1 — number value to copy the magnitude from. 2 — number value to copy the sign from.

copySign(integer, integer) integer

Gets the number with the magnitude of the first argument and the sign of the second argument.
Returns an integer.


Arguments: 1 — integer value to copy the magnitude from. 2 — integer value to copy the sign from.

String operations

Function

Description

len(string) integer

Gets the length of the specified string.
Returns an integer.


Argument: string value.

contains(string, string) boolean

Determines whether the string (Argument 1) contains the specified substring (Argument 2).
Returns a boolean.


Arguments: 1 — Source string to search. 2 — string substring to search for.

substring(string, integer, integer) string

Gets the part of the string (Argument 1) located in the range defined by Arguments 2 and 3.
Returns a string.


Arguments: 1 — Source string. 2 — integer value of the copied substring's left index. 3 — integer value of the copied substring's right index.

replaceAll(string, string, string) string

Finds and replaces all occurrences of the substring (Argument 2) in the source string (Argument 1) with the string specified in Argument 3.
Returns a string copy of the source string with all the matches replaced.


Arguments: 1 — Source string to search. 2 — string substring to search for. 3 — Replacement string.

index(string, string) integer

Gets the index of the first occurrence of the substring (Argument 2) in the string (Argument 1).
Returns an integer: the index of the first occurrence or -1 if the substring is not found.


Arguments: 1 — Source string to search. 2 — string substring to search for.

lastIndex(string, string) integer

Gets the index of the last occurrence of the substring (Argument 2) in the string (Argument 1).
Returns an integer: the index of the last occurrence or -1 if the substring is not found.


Arguments: 1 — Source string to search. 2 — string substring to search for.

trim(string) string

Trims whitespace from both ends of the specified string.
Returns a string.


Argument: string value.

trimLeft(string) string

Trims whitespace from the beginning of the specified string.
Returns a string.


Argument: string value.

trimRight(string) string

Trims whitespace from the end of the specified string.
Returns a string.


Argument: string value.

toUpperCase(string) string

Converts the specified string to uppercase.
Returns a string.


Argument: string value.

toLowerCase(string) string

Converts the specified string to lowercase.
Returns a string.


Argument: string value.

padStart(string, integer, string) string

Pads the beginning of the string with the specified characters until it reaches the specified length.
Returns a string.


Arguments: 1 — Source string. 2 — integer value of the required string length. 3 — string to pad the beginning of the source string with.


For example: "@{padStart('0', 4, '12')}""1210"

padStart(integer, integer, string) string

Converts the number to a string and pads the beginning of the resulting string with the specified characters until it reaches the specified length.
Returns a string.


Arguments: 1 — Source integer. 2 — integer value of the required string length. 3 — string to pad the beginning of the resulting string with.


For example: "@{padStart(0, 4, '12')}""1210"

padEnd(string, integer, string) string

Pads the end of the string with the specified characters until it reaches the specified length.
Returns a string.


Arguments: 1 — Source string. 2 — integer value of the required string length. 3 — string to pad the end of the source string with.


For example: "@{padEnd('0', 4, '12')}""0121"

padEnd(integer, integer, string) string

Converts the number to a string and pads the end of the resulting string with the specified characters until it reaches the specified length.
Returns a string.


Arguments: 1 — Source integer. 2 — integer value of the required string length. 3 — string to pad the end of the resulting string with.


For example: "@{padEnd(0, 4, '12')}""0121"

Color operations

Function

Description

getColorAlpha(string) number

Gets the alpha component of the specified color.
Returns a number in the range from 0.0 to 1.0.


Argument: string hex color value.

getColorRed(string) number

Gets the red component of the specified color.
Returns a number in the range from 0.0 to 1.0.


Argument: string hex color value.

getColorGreen(string) number

Gets the green component of the specified color.
Returns a number in the range from 0.0 to 1.0.


Argument: string hex color value.

getColorBlue(string) number

Gets the blue component of the specified color.
Returns a number in the range from 0.0 to 1.0.


Argument: string hex color value.

setColorAlpha(string, number) string

Sets the alpha value of the specified color.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — string hex color value. 2 — number alpha value in the range from 0.0 to 1.0.

setColorRed(string, number) string

Sets the red value of the specified color.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — string hex color value. 2 — number red value in the range from 0.0 to 1.0.

setColorGreen(string, number) string

Sets the green value of the specified color.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — string hex color value. 2 — number green value in the range from 0.0 to 1.0.

setColorBlue(string, number) string

Sets the blue value of the specified color.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — string hex color value. 2 — number blue value in the range from 0.0 to 1.0.

argb(number, number, number, number) string

Creates a hex color code with the specified alpha, red, green, and blue values.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — number alpha value in the range from 0.0 to 1.0. 2 — number red value in the range from 0.0 to 1.0. 3 — number green value in the range from 0.0 to 1.0. 4 — number blue value in the range from 0.0 to 1.0.

rgb(number, number, number) string

Creates a hex color code with the specified red, green, and blue values. Sets the alpha component value to 1.0.
Returns a string hex color value. For example, '#FFAABBCC'.


Arguments: 1 — number red value in the range from 0.0 to 1.0. 2 — number green value in the range from 0.0 to 1.0. 3 — number blue value in the range from 0.0 to 1.0.

Date and time operations

Function

Description

parseUnixTime(integer) datetime

Creates a datetime value from the specified Unix timestamp.
Returns a datetime.


Argument: integer number of seconds elapsed since midnight, January 1, 1970, UTC.

nowLocal() datetime

Creates a datetime value for the current time.
Returns a datetime.

No arguments.

addMillis(datetime, milliseconds: integer) datetime

Adds the specified number of milliseconds to the original datetime.
Returns a datetime.


Arguments: 1 — Original datetime. 2 — integer number of milliseconds to add.

setYear(datetime, year: integer) datetime

Sets the year value of the original datetime.
Returns a datetime with the specified year value.


Arguments: 1 — Original datetime. 2 — integer year value.

setMonth(datetime, month: integer) datetime

Sets the month value of the original datetime.
Returns a datetime with the specified month value.


Arguments: 1 — Original datetime. 2 — integer month value from 1 to 12.

setDay(datetime, monthDay: integer) datetime

Sets the day of month value of the original datetime.
Returns a datetime with the specified day of month value.


Arguments: 1 — Original datetime. 2 — integer day of month value.

setHours(datetime, hours: integer) datetime

Sets the hours value of the original datetime.
Returns a datetime with the specified hours value.


Arguments: 1 — Original datetime. 2 — integer hours value.

setMinutes(datetime, minutes: integer) datetime

Sets the minutes value of the original datetime.
Returns a datetime with the specified minutes value.


Arguments: 1 — Original datetime. 2 — integer minutes value.

setSeconds(datetime, seconds: integer) datetime

Sets the seconds value of the original datetime.
Returns a datetime with the specified seconds value.


Arguments: 1 — Original datetime. 2 — integer seconds value.

setMillis(datetime, milliseconds: integer) datetime

Sets the milliseconds value of the original datetime.
Returns a datetime with the specified milliseconds value.


Arguments: 1 — Original datetime. 2 — integer milliseconds value.

getYear(datetime) integer

Gets the year value of the original datetime.
Returns an integer.


Argument: Original datetime.

getMonth(datetime) integer

Gets the month number value of the original datetime.
Returns an integer.


Argument: Original datetime.

getDay(datetime) integer

Gets the day of month value of the original datetime.
Returns an integer.


Argument: Original datetime.

getDayOfWeek(datetime) integer

Gets the day of week value of the original datetime, where 7 is Sunday.
Returns an integer.


Argument: Original datetime.

getHours(datetime) integer

Gets the hours value of the original datetime.
Returns an integer.


Argument: Original datetime.

getMinutes(datetime) integer

Gets the minutes value of the original datetime.
Returns an integer.


Argument: Original datetime.

getSeconds(datetime) integer

Gets the seconds value of the original datetime.
Returns an integer.


Argument: Original datetime.

getMillis(datetime) integer

Gets the milliseconds value of the original datetime.
Returns an integer.


Argument: Original datetime.

getIntervalSeconds(integer) integer

Converts the interval in milliseconds to Dd HH:MM:SS format (days, hours, minutes, seconds) and gets its seconds value.
Returns an integer in the range from 0 to 59.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalSeconds(100000000)}40

getIntervalTotalSeconds(integer) integer

Converts the interval in milliseconds to seconds and rounds the result down.
Returns an integer seconds value.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalTotalSeconds(100000000)}100000

getIntervalMinutes(integer) integer

Converts the interval in milliseconds to Dd HH:MM:SS format (days, hours, minutes, seconds) and gets its minutes value.
Returns an integer in the range from 0 to 59.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalMinutes(100000000)}46

getIntervalTotalMinutes(integer) integer

Converts the interval in milliseconds to minutes and rounds the result down.
Returns an integer minutes value.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalTotalMinutes(100000000)}1666

getIntervalHours(integer) integer

Converts the interval in milliseconds to Dd HH:MM:SS format (days, hours, minutes, seconds) and gets its hours value.
Returns an integer in the range from 0 to 23.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalHours(100000000)}3

getIntervalTotalHours(integer) integer

Converts the interval in milliseconds to hours and rounds the result down.
Returns an integer hours value.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalTotalHours(100000000)}27

getIntervalTotalDays(integer) integer

Converts the interval in milliseconds to days and rounds the result down.
Returns an integer day value.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalTotalDays(1000000000)}11

getIntervalTotalWeeks(integer) integer

Converts the interval in milliseconds to weeks and rounds the result down.
Returns an integer week value.


Argument: integer interval duration in milliseconds.

For example, @{getIntervalTotalWeeks(1000000000)}1

Learn more

Follow DivKit news in the Telegram channel: http://t.me/divkit_news

You can also discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_ru

DivKit Repository