FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions.

Trunc

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

Trunc( value, [precision] )
trunc( value, [precision] )
Trunc( value, [precision] )
Trunc( value, [precision] )
Trunc( value, [precision] )

Description

The Trunc function returns a number which is the nearest mathematic value less than or equal to the operand to the specified precision as a double value.

Parameters

Parameter Type Definition and Requirements

value

Number

A Number to be truncated with a specific precision.

precision

Number

Optional - Defines how many digits to the right or left of the decimal place should be returned. The default precision is 2 which returns up to the hundredths decimal places. A positive precision specifies digits to the right of the decimal point. A negative precision specifies digits to the left of the decimal point. A zero precision truncates the fractional part of the number.

Since Go does not have function overloading or optional arguments, you must use the function f.Precision() instead of a plain number.

Returns

A Number based on value using the specified precision.

Examples

The following query executes an array of independent truncation operations and returns the results in an array. The result array position matches the execution array position. The first operation takes the value 1234.5678 and by default truncates the value to 2 digits to the right of the decimal point. The result is 1234.56 which is in the top position of the result array. The second operation truncates the value 1234 to a precision of -2. This is two digits to the left of the decimal point, the hundreds position. The operations to the left of the decimal point are zero filled less than the specified precision. The result of 1200 is placed in the third position of the result array.

client.query(
  [
    q.Trunc(1234.5678),
    q.Trunc(1234, -2),
    q.Trunc(1234.5678, 2),
    q.Trunc(1234.5678, -2),
    q.Trunc(5678.1234, 3),
    q.Trunc(1234, -3),
    q.Trunc(5678.1234, -3),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[
   1234.56, 1200,
   1234.56, 1200,
  5678.123, 1000,
      5000
]
result = client.query(
  [
    q.trunc(1234.5678),
    q.trunc(1234, -2),
    q.trunc(1234.5678, 2),
    q.trunc(1234.5678, -2),
    q.trunc(5678.1234, 3),
    q.trunc(1234, -3),
    q.trunc(5678.1234, -3)
  ]
)
print(result)
[1234.56, 1200, 1234.56, 1200.0, 5678.123, 1000, 5000.0]
result, err := client.Query(
	f.Arr{
		f.Trunc(1234.5678),
		f.Trunc(1234, f.Precision(-2)),
		f.Trunc(1234.5678, f.Precision(2)),
		f.Trunc(1234.5678, f.Precision(-2)),
		f.Trunc(5678.1234, f.Precision(3)),
		f.Trunc(1234, f.Precision(-3)),
		f.Trunc(5678.1234, f.Precision(-3)),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[1234.56 1200 1234.56 1200 5678.123 1000 5000]
try
{
    Value result = await client.Query(
        Arr(
            Trunc(1234.5678),
            Trunc(1234, -2),
            Trunc(1234.5678, 2),
            Trunc(1234.5678, -2),
            Trunc(5678.1234, 3),
            Trunc(1234, -3),
            Trunc(5678.1234, -3)
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(DoubleV(1234.56), LongV(1200), DoubleV(1234.56), DoubleV(1200), DoubleV(5678.123), LongV(1000), DoubleV(5000))
[
  Trunc(1234.5678),
  Trunc(1234, -2),
  Trunc(1234.5678, 2),
  Trunc(1234.5678, -2),
  Trunc(5678.1234, 3),
  Trunc(1234, -3),
  Trunc(5678.1234, -3),
]
[
   1234.56, 1200,
   1234.56, 1200,
  5678.123, 1000,
      5000
]
Query metrics:
  •    bytesIn: 219

  •   bytesOut:  63

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 5ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!