# How do I convert a python float to a Float32?

**URL:** <https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645>\
**Category:** Mojo\
**Tags:** python-interop\
**Created:** [January 27, 2026, 9:47pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645 "2026-01-27T21:47:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jweston](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jweston](https://forum.modular.com/u/Jweston)\
**Post date:** [January 27, 2026, 9:47pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/1 "2026-01-27T21:47:44Z")

</div>

I am trying to iterate over a Python list and compare the values to those in a Mojo `Float32` list, but I have not found a way of converting a Python `float` to any Mojo type for comparison.

I’ve tried `Float32(f.to_float64())`, but `to_float64()` returns a `PythonObject`, and `Float32` cannot convert a `PythonObject`. The same happens with `Float64(f)`. Replacing the `Float##` with `SIMD[DType.float##]` also doesn’t work, and removing the `to_float64()` doesn’t work either.

Does anyone know what I am doing wrong or how I can fix this?

---

<div class="post-metadata">

**Author:** ![Doby](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Doby](https://forum.modular.com/u/Doby)\
**Post date:** [January 29, 2026, 1:15am UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/2 "2026-01-29T01:15:57Z")

</div>

It’s gotta be here: [Python types | Modular](https://docs.modular.com/mojo/manual/python/types/#mojo-wrapper-objects)

---

<div class="post-metadata">

**Author:** ![sora](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sora](https://forum.modular.com/u/sora)\
**Post date:** [January 29, 2026, 4:05am UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/3 "2026-01-29T04:05:24Z")

</div>

`PythonObject.to_float64()` was deprecated in 25.3 (2025-05-06) and has since been removed. Could you try upgrading? This code should run:

```mojo
from python import PythonObject

def main():
  o = PythonObject(1.0)
  _ = Float32(py=o)
  _ = Float64(py=o)

```

---

<div class="post-metadata">

**Author:** ![spluta](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@spluta](https://forum.modular.com/u/spluta)\
**Post date:** [January 30, 2026, 10:41pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/4 "2026-01-30T22:41:21Z")

</div>

I have the same problem [here](https://github.com/spluta/MMMAudio/blob/8528f3e31d9b76f6240138d3b124b01628d790c7/mmm_audio/MLP_Module.mojo#L128) on line 128 (currently commented out).

My solution in line 129 is uhhhh, not good.

There are some spots in the codebase where `Float64(py=py_val)` works fine and some places where it doesn’t. What am I not understanding?

Thanks,

Sam

---

<div class="post-metadata">

**Author:** ![spluta](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@spluta](https://forum.modular.com/u/spluta)\
**Post date:** [January 31, 2026, 2:35pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/5 "2026-01-31T14:35:28Z")

</div>

I was able to find a solution to this that might help someone else. I made a separate .mojo file with the following function in it:

```auto
fn py_to_float64(py_float: PythonObject) raises -> Float64:
    return Float64(py=py_float)

```

and I imported this file to the file where I needed the conversion to happen. Works great.

---

<div class="post-metadata">

**Author:** ![Jweston](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@Jweston](https://forum.modular.com/u/Jweston)\
**Post date:** [February 5, 2026, 5:33pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/6 "2026-02-05T17:33:02Z")

</div>

Sorry for the delay, I thought I already sent this. This solution works! I suppose the issue I was having before was that I wasn’t including `PythonObject`. It seems a strange requirement given that I had already been interacting with `PythonObject`s. I also had been using 26.0.2. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [February 12, 2026, 5:33pm UTC](https://forum.modular.com/t/how-do-i-convert-a-python-float-to-a-float32/2645/7 "2026-02-12T17:33:20Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
