The to-float operation converts the input value into a floating-point number (decimal number). It is based on PHP’s type casting rules for floats.
How It Works
- Integer → Float
- Whole numbers are converted to their decimal equivalent.
- Example: 42 → 42.0
- String containing a number → Float
- Numeric strings are converted to a float.
- Example: “1.25” → 1.25
- Boolean → Float
- true converts to 1.0
- false converts to 0.0
- Null → Float
- null converts to 0.0
- Non-numeric strings → Float
- If the string does not start with a digit, the result is 0.0.
- Example: “abc123” → 0.0
- If the string starts with digits, PHP attempts to parse them as a number.
- Example: “123abc” → 123.0
- Arrays or objects → Error
- Arrays and objects cannot be converted into floats. Attempting to do so will cause an error.
Examples
Input | Result |
---|---|
42 | 42.0 |
“1.25” | 1.25 |
“123abc” | 123.0 |
“abc123” | 0.0 |
true | 1.0 |
false | 0.0 |
null | 0.0 |
[1,2,3] | ❌ Error |
{ foo: “bar” } | ❌ Error |
Usage in PalDock
Use to-float when:
- An API requires numeric values in float format.
- You need to ensure consistent numeric formatting before sending data.
- A field may contain string numbers that should be interpreted as floats.
⚠ Note: Be cautious with mixed strings like “123abc”. They will partially convert, which may not always be the desired behavior. If you require strict numeric-only values, combine to-float with validation rules.