Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
"""Number Convert node for unified numeric type conversion.
|
|
|
|
|
|
|
|
|
|
Provides a single node that converts INT, FLOAT, STRING, and BOOL
|
|
|
|
|
inputs into FLOAT and INT outputs.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
from typing_extensions import override
|
|
|
|
|
|
|
|
|
|
from comfy_api.latest import ComfyExtension, io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NumberConvertNode(io.ComfyNode):
|
|
|
|
|
"""Converts various types to numeric FLOAT and INT outputs."""
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def define_schema(cls) -> io.Schema:
|
|
|
|
|
return io.Schema(
|
|
|
|
|
node_id="ComfyNumberConvert",
|
2026-05-19 12:13:48 +08:00
|
|
|
display_name="Convert Number",
|
2026-05-27 17:43:33 -07:00
|
|
|
category="utilities",
|
Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
search_aliases=[
|
|
|
|
|
"int to float", "float to int", "number convert",
|
|
|
|
|
"int2float", "float2int", "cast", "parse number",
|
|
|
|
|
"string to number", "bool to int",
|
|
|
|
|
],
|
|
|
|
|
inputs=[
|
|
|
|
|
io.MultiType.Input(
|
|
|
|
|
"value",
|
|
|
|
|
[io.Int, io.Float, io.String, io.Boolean],
|
|
|
|
|
display_name="value",
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
outputs=[
|
|
|
|
|
io.Float.Output(display_name="FLOAT"),
|
|
|
|
|
io.Int.Output(display_name="INT"),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def execute(cls, value) -> io.NodeOutput:
|
|
|
|
|
if isinstance(value, bool):
|
|
|
|
|
float_val = 1.0 if value else 0.0
|
2026-03-26 07:06:34 +09:00
|
|
|
int_val = 1 if value else 0
|
|
|
|
|
elif isinstance(value, int):
|
Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
float_val = float(value)
|
2026-03-26 07:06:34 +09:00
|
|
|
int_val = value
|
|
|
|
|
elif isinstance(value, float):
|
|
|
|
|
float_val = value
|
|
|
|
|
int_val = int(value)
|
Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
elif isinstance(value, str):
|
|
|
|
|
text = value.strip()
|
|
|
|
|
if not text:
|
|
|
|
|
raise ValueError("Cannot convert empty string to number.")
|
|
|
|
|
try:
|
|
|
|
|
float_val = float(text)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Cannot convert string to number: {value!r}"
|
|
|
|
|
) from None
|
2026-03-26 07:06:34 +09:00
|
|
|
if not math.isfinite(float_val):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Cannot convert non-finite value to number: {float_val}"
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
int_val = int(text)
|
|
|
|
|
except ValueError:
|
|
|
|
|
int_val = int(float_val)
|
Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
else:
|
|
|
|
|
raise TypeError(
|
|
|
|
|
f"Unsupported input type: {type(value).__name__}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not math.isfinite(float_val):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Cannot convert non-finite value to number: {float_val}"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-26 07:06:34 +09:00
|
|
|
return io.NodeOutput(float_val, int_val)
|
Add Number Convert node (#13041)
* Add Number Convert node for unified numeric type conversion
Consolidates fragmented IntToFloat/FloatToInt nodes (previously only
available via third-party packs like ComfyMath, FillNodes, etc.) into
a single core node.
- Single input accepting INT, FLOAT, STRING, and BOOL types
- Two outputs: FLOAT and INT
- Conversion: bool→0/1, string→parsed number, float↔int standard cast
- Follows Math Expression node patterns (comfy_api, io.Schema, etc.)
Refs: COM-16925
* Register nodes_number_convert.py in extras_files list
Without this entry in nodes.py, the Number Convert node file
would not be discovered and loaded at startup.
* Add isfinite guard, exception chaining, and unit tests for Number Convert node
- Add math.isfinite() check to prevent int() crash on inf/nan string inputs
- Use 'from None' for cleaner exception chaining on string parse failure
- Add 21 unit tests covering all input types and error paths
2026-03-25 07:38:08 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class NumberConvertExtension(ComfyExtension):
|
|
|
|
|
@override
|
|
|
|
|
async def get_node_list(self) -> list[type[io.ComfyNode]]:
|
|
|
|
|
return [NumberConvertNode]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def comfy_entrypoint() -> NumberConvertExtension:
|
|
|
|
|
return NumberConvertExtension()
|