todouble, toreal
This page explains how to use the todouble function in APL.
Use the todouble function (or its synonym toreal) to convert various data types to a real (floating-point) number. This is helpful when you need to normalize numeric values from different sources into decimal format for mathematical operations, comparisons, or aggregations.
You typically use todouble when working with numeric strings, integers, or other types that need to be converted to floating-point numbers for precise calculations or when decimal precision is required.
Usage
Syntax
Parameters
| Name | Type | Description |
|---|---|---|
| value | dynamic | The value to convert to real. |
Returns
If conversion is successful, the result is a value of type real. If conversion isn't successful, the result is null.
Conversion behavior
The todouble function converts values based on their type:
- Integer: Converted to float. For example,
1becomes1.0,-1becomes-1.0. - String: Parsed as a 64-bit float using Go floating-point literal syntax, which supports scientific notation. For example,
"1e3"becomes1000.0. - Boolean:
truebecomes1.0,falsebecomes0.0 - Datetime: Converted to nanoseconds since epoch as a float
- Duration: Converted to float nanoseconds
Use case examples
Convert string representations of numeric values to real numbers for mathematical calculations and aggregations.
Query
Output
| _time | uri | req_duration_ms | duration_seconds | is_slow |
|---|---|---|---|---|
| Jun 24, 09:28:10 | /api/users | 1500 | 1.5 | true |
This example converts milliseconds to seconds using todouble to ensure decimal precision in the calculation, enabling accurate time-based analysis.
Convert trace duration values to real numbers for precise duration calculations and percentile analysis.
Query
Output
| _time | trace_id | service.name | duration | duration_ms | is_slow_span |
|---|---|---|---|---|---|
| Jun 24, 09:28:10 | abc123 | frontend | 150000000 | 150.0 | true |
This example converts nanosecond durations to milliseconds using todouble to maintain decimal precision, enabling accurate performance analysis of trace spans.
Convert numeric security metrics to real numbers for threshold-based security analysis and alerting.
Query
Output
| _time | uri | status | req_duration_ms | risk_score | is_high_risk |
|---|---|---|---|---|---|
| Jun 24, 09:28:10 | /admin | 403 | 5500 | 55.0 | true |
This example converts request duration to a risk score using todouble to enable precise threshold-based security analysis with decimal precision.
List of related functions
- toreal: Synonym for
todouble. Both functions convert values to real numbers. - toint: Converts input to integer. Use
tointwhen you need whole numbers, andtodoublewhen you need decimal precision.