ToInteger

ToInteger( value )
ToInteger( value )
ToInteger( value )
ToInteger( value )
to_integer( value )
ToInteger( value )

Description

The ToInteger function converts a value to a numeric integer, if possible.

Attempting to convert a value to a number which has no numeric representation results in an "invalid argument" error.

Parameters

Parameter Type Definition and Requirements

value

Any

The value to attempt to convert to an Integer.

Returns

An Integer that corresponds to value.

Examples

The following query uses ToInteger multiple times to demonstrate how the function converts various kinds of values into integers:

try
{
    Value result = await client.Query(
        Arr(
            ToInteger(1234.5678),
            ToInteger(1234),
            ToInteger("123")
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(LongV(1234), LongV(1234), LongV(123))
result, err := client.Query(
	f.Arr{
		f.ToInteger(1234.5678),
		f.ToInteger(1234),
		f.ToInteger("123"),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[1234 1234 123]
System.out.println(
    client.query(
        Arr(
            ToInteger(Value(1234.5678)),
            ToInteger(Value(1234)),
            ToInteger(Value("123"))
        )
    ).get());
[1234, 1234, 123]
client.query(
  [
    q.ToInteger(1234.5678),
    q.ToInteger(1234),
    q.ToInteger('123'),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 1234, 1234, 123 ]
result = client.query(
  [
    q.to_integer(1234.5678),
    q.to_integer(1234),
    q.to_integer('123'),
  ]
)
print(result)
[1234, 1234, 123]
[
  ToInteger(1234.5678),
  ToInteger(1234),
  ToInteger('123'),
]
[ 1234, 1234, 123 ]
Query metrics:
  •    bytesIn:  67

  •   bytesOut:  28

  • 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!