Nodes/ComfyUI-My-Nodes/浮点/张量转整数
ComfyUI Node

浮点/张量转整数

Round floats or tensors to integers, four rounding modes included

By Tagbliton·Created 2 years ago·Updated about a year ago· 11
浮点/张量转整数
  • image
  • image
  • int
float0.00
mode

Two conversion jobs, one node: turn a FLOAT into an INT, or turn a tensor of floats into a tensor of int64s - with your pick of four rounding modes. When your workflow computes a value that has to become a batch size, a step count, or an index, something has to do the rounding. This node is that something, and the name is the first thing to know: "Terson" is a typo for Tensor, so the title means "Float/Tensor To Int." Yes, it's misspelled in the pack. No, they haven't fixed it. It's part of the charm, and searching "float to int" will still find it.

The mechanism is a clean mode switch over four behaviors: Round (round half up), Floor (down), Ceil (up), Truncate (toward zero). It applies to whichever input you've connected. Feed a FLOAT into float and the int output comes out rounded per your mode. Feed a tensor into image (yes, the input is awkwardly named image - it's *-typed, so it accepts any tensor, not just images) and the image output comes back as int64 tensors.

Outputs: image (the converted tensor, if you fed a tensor in) and int (the converted scalar, if you fed a float in).

The two quirks are worth spelling out because they'll bite you. First: float is checked for truthiness, not presence - if you leave it at its default 0, the scalar path doesn't run, and you get int = None on the output while image returns a dummy 1×1×1×4 tensor of zeros. So connect either the float or the tensor, but don't expect a meaningful output from an unset float. Second, the node converts the entire tensor with torch.round/floor/ceil/trunc and then casts to int64 - element-wise, all at once - which is exactly what you want for a batch of values (like a list of step counts) without a per-element loop.

Where it fits: computing a batch count from a fractional value, converting a denoise-strength float into an index for a lookup, or any place a downstream node refuses a float where an int belongs.

Install: part of ComfyUI-My-Nodes - ComfyUI Manager → search "ComfyUI-My-Nodes", or git clone https://github.com/Tagbliton/ComfyUI-My-Nodes into custom_nodes and restart. Local utility - no API key, no network.

Gotchas. The float-defaults-to-0 trap above is the main one; if int comes back None, that's why. Truncate vs Floor also trips people up on negative numbers - truncate goes toward zero (-2.7-2), floor goes down (-2.7-3). And remember the tensor output is int64: if a downstream node expects a float tensor, you'll need a convert node after this one. If you only ever need plain scalar rounding, the pack's simpler tools or ComfyUI's built-in math primitives do the same job with fewer surprises - this node earns its keep when a tensor's involved.

Category我的节点/Tools

Inputs (3)

NameTypeDefaultDescription
imageopt*
floatoptFLOAT0.00-9999–9999
modeoptCOMBO4 options: Round, Floor, Ceil, Truncate

Outputs (2)

NameTypeDescription
image*
intINT