ToNumber

ToNumber( value )
ToNumber( value )
ToNumber( value )
ToNumber( value )
to_number( value )
ToNumber( value )

Description

The ToNumber function converts a value to a numeric literal, 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 a Number.

Returns

A Number created by interpreting value.

Examples

The following query executes an array of independent ToNumber operations and returns the results in an array. The result array position matches the execution array position. The first operation converts a floating point literal to a number. The second operation takes an integer literal and converts it to a number. The last operation converts a string to a number.

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

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

  •   bytesOut:  33

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

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