xs:float

Converts $string-var (a string) to a 32 bit floating point value. The data type float corresponds to the IEEE single-precision 32-bit floating point type (IEEE Std 754-1985).

If the value of $string-var is the empty sequence, the empty sequence is returned. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.

Signatures

xs:float(xs:string $string-var) —> xs:float

Arguments

Data Type
Argument
Description

xs:string

$string-var

Represents the string to convert to a float.

Returns

Returns the floating point value of $string-var.

Examples

Simple

Invoking float("1.1")returns the floating point value of 1.1 as shown in the following example query:

<float>{xs:float("1.1")}</float> 

The preceding query generates the following result:

<float>1.1</float> 

Exponent

Invoking float("-104.345e2")returns the floating point value of -10434.5 as shown in the following example query:

<float>{xs:float("-104.345e2")}</float> 

The preceding query generates the following error:

<float>-10434.5</float> 

NaN

The string: NaN (Not a Number) is a legal argument, as shown in the following example query:

<float>{xs:float("NaN")}</float> 

The preceding query generates the following result:

<float>NaN</float> 

INF and -INF

The strings: INF (positive infinity) and -INF (negative infinity) are legal arguments as shown in the following example query:

<infinity>
	<positive>{xs:float("INF")}</positive>
	<negative>{xs:float("-INF")}</negative>
</infinity> 

The preceding query generates the following result:

<infinity>
	<positive>Infinity</positive>
	<negative>-Infinity</negative>
</infinity> 

Error—Invalid Exponent

Invoking float("10.1e2.1") outputs an error because 2.1 is not an integer. For example, the following example query:

<float>{xs:float("10.1e2.1")}</float> 

Produces the following error:

Error occurred while executing XQuery: Could not cast "10.1e2.1" to type [float@http://www.w3.org/2001/XMLSchema] 

Null

Invoking float(()) returns the empty sequence. The empty sequence is a sequence containing zero items (), which is similar to null in SQL.

For example, the following example query:

<float>{xs:float(())}</float> 

The preceding query generates the following result:

<float/> 

Related Topics

W3C float data type description