How to Change a Tensor's Data Type in TensorFlow with tf.cast
TensorFlow won't mix data types in one calculation: adding a whole-number tensor to a decimal one fails. tf.cast converts a tensor to the type you need. This guide casts decimals to whole numbers and back in TensorFlow 2.22.0, and shows what happens to values that don't fit the new type.
Syntax
tf.cast(<tensor>, <data type>)
| Part | What it means |
|---|---|
<tensor> | The tensor to convert |
<data type> | The new type, such as tf.int32, tf.float32 or tf.uint8 |
The tf.cast API page says it "Casts a tensor to a new type", and its own example shows decimals losing their fraction: [1.8, 2.2] cast to tf.int32 gives [1, 2]. tf.cast returns a new tensor; the original stays as it was.
The Example
Three prices with cents, cast to whole numbers, and three counts, cast to decimal numbers.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
prices = tf.constant([1.25, 2.99, 7.5])
print(tf.cast(prices, tf.int32))
counts = tf.constant([1, 2, 3])
print(tf.cast(counts, tf.float32))

prices is float32, so the first cast goes from decimals to whole numbers. counts is int32, so the second goes the other way.
Step 2: Run It
uv run python cast_demo.py
tf.Tensor([1 2 7], shape=(3,), dtype=int32)
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32)

The prices became [1 2 7]: casting to a whole-number type cut off the fraction, so 2.99 became 2, not 3. The counts became [1. 2. 3.], the same values as decimals, now float32. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Casting values that don't fit the new type | tf.cast(tf.constant([-1.7, 300.0]), tf.uint8) gave [255 44]. uint8 holds 0 to 255, and the cast didn't check: both values came out wrong | Use tf.dtypes.saturate_cast, which clamped them to [ 0 255] |
Expecting tf.cast to round | 2.99 became 2: the fraction is cut off | Round first with tf.round, then cast: the prices became [1 3 8]. tf.round sends halves to the even number, so 0.5, 1.5 and 2.5 became 0., 2. and 2. |
The saturate_cast API page describes the safer cast: "This function casts the input to dtype without overflow. If there is a danger that values would over or underflow in the cast, this op applies the appropriate clamping before the cast."
FAQ
Does casting to float32 lose anything?
Not for small whole numbers like these: 1, 2, 3 became exactly 1. 2. 3.. Very large ones can lose their last digits: 16777217 cast to float32 became 16777216.0.
Why cast photos to float32 before calculating with them?
Photos load as uint8, which holds only 0 to 255 and no fractions. Most maths on them, such as scaling to between 0 and 1, needs decimals, so cast to float32 first. Dividing the uint8 photo by 255.0 straight away fails, as Fixed: TypeError: x and y must have the same dtype, got tf.uint8 != tf.float32 shows.
Conclusion
tf.cast turned decimal prices into whole numbers, cutting off the fraction, and whole counts into decimals. It doesn't check whether values fit the new type, so tf.dtypes.saturate_cast is the safer choice when they might not.
Additional Resources
- tf.cast — the types it converts between.
- tf.dtypes.saturate_cast — a cast that clamps values to the new type's range.
