43 lines
1.3 KiB
Plaintext
43 lines
1.3 KiB
Plaintext
:sig
|
|
f64 DValue::to_f64(f64 default_value = 0) const
|
|
|
|
:params
|
|
default_value : returned when the value is missing or cannot be parsed as a number
|
|
return value : the value as a floating-point number, or `default_value`
|
|
|
|
:see
|
|
>types
|
|
0_DValue
|
|
json_decode
|
|
float_val
|
|
to_bool
|
|
to_s64
|
|
to_u64
|
|
|
|
:content
|
|
Reads a `DValue` value as a floating-point number. This is a read accessor: it is `const`, never creates or modifies nodes, and dereferences internal references automatically.
|
|
|
|
String values are trimmed and parsed permissively: numeric forms and the boolean words understood by `to_bool()` both convert. Boolean values become `1.0` or `0.0`.
|
|
|
|
`default_value` is returned when:
|
|
|
|
- the node is unset or holds an empty string
|
|
- the string does not parse as a finite number
|
|
- the node is map-shaped with more than one entry, or an unresolvable reference
|
|
|
|
A map-shaped node with exactly one entry unwraps to that entry's value before converting.
|
|
|
|
## Example
|
|
|
|
```cpp
|
|
f64 ratio = context.props["ratio"].to_f64(1.0);
|
|
f64 threshold = context.cfg.get_by_path("alerts/threshold").to_f64(0.75);
|
|
```
|
|
|
|
Use this for numeric config values, JSON-decoded fields, component props, and request data that should be treated as a number.
|
|
|
|
## Related Concepts
|
|
|
|
- PHP: `floatval()` with a fallback
|
|
- JavaScript: `parseFloat(x) || fallback`
|