trunc

..  trunc([T,] x, [digits, [base]])

``trunc(x)`` returns the nearest integral value of the same type as ``x`` whose absolute
value is less than or equal to ``x``.

``trunc(T, x)`` converts the result to type ``T``, throwing an
``InexactError`` if the value is not representable.

``digits`` and ``base`` work as for :func:`round`.

Examples

In the Julia programming language, the function trunc is used to obtain the nearest integral value of the same type as x whose absolute value is less than or equal to x. The function can be used in different ways, as described below.

  1. Truncate a floating-point number to the nearest integer:

    julia> trunc(3.8)
    3

    This example returns the nearest integral value less than or equal to 3.8, which is 3.

  2. Convert the result to a specific type:

    julia> trunc(Int64, 4.9)
    4

    The trunc function can also convert the result to a specific type. In this example, the result is converted to an Int64.

  3. Specify the number of digits and base:
    julia> trunc(3.14159, digits=2, base=10)
    3.14

    The digits argument specifies the number of digits to which the result should be truncated, and the base argument determines the base for rounding. In this case, the result is truncated to 2 digits after the decimal point with a base of 10.

It's important to note that if the resulting value is not representable in the specified type T, using trunc(T, x) will throw an InexactError exception.

Please let me know if there's anything else I can assist you with!

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: