---
title: Type Conversions
description: Implicit and explicit conversions between Slint types.
---

import Link from '@slint/common-files/src/components/Link.astro';

## Type Conversions

Slint supports conversions between different types. Explicit
conversions are required to make the UI description more robust, but implicit
conversions are allowed between some types for convenience.

The following conversions are possible:

-   `int` can be converted implicitly to `float` and vice-versa.
     When converting from `float` to `int`, the value is truncated.
-   `int` and `float` can be converted implicitly to `string`,
    and `string` can be converted to `float` with the `to-float()` member function.
    See [Converting Between Numbers and Strings](#converting-between-numbers-and-strings).
-   `physical-length`, `relative-font-size`, and `length` can be converted implicitly to each other only in
    context where the pixel ratio is known.
-   the units type (`length`, `physical-length`, `duration`, ...) can't be converted to numbers (`float` or `int`)
    but they can be divided by themselves to result in a number. Similarly, a number can be multiplied by one of
    these unit. The idea is that one would multiply by `1px` or divide by `1px` to do such conversions
-   The literal `0` can be converted to any of these types that have associated unit.
-   Struct types convert with another struct type if they have the same property names and their types can be converted.
    The source struct can have either missing properties, or extra properties. But not both.
-   Arrays generally don't convert between each other. Array literals can be converted if the element types are convertible.

```slint
export component Example {
    // OK: int converts to string
    property<{a: string, b: int}> prop1: {a: 12, b: 12 };
    // OK: even if a is missing, it will just have the default value ("")
    property<{a: string, b: int}> prop2: { b: 12 };
    // OK: even if c is too many, it will be discarded
    property<{a: string, b: int}> prop3: { a: "x", b: 12, c: 42 };
    // ERROR: b is missing and c is extra, this doesn't compile, because it could be a typo.
    // property<{a: string, b: int}> prop4: { a: "x", c: 42 };

    property<string> xxx: "42.1";
    property<float> xxx1: xxx.to-float(); // 42.1
    property<bool> xxx2: xxx.is-float(); // true
    property<int> xxx3: 45.8; // 45
}
```

### Converting Between Numbers and Strings

`int` and `float` convert implicitly to `string`.
To control how many digits appear in the result, use the [`to-fixed()` and `to-precision()`](../../property-types/numeric-types/#to-fixeddigits-int---string)
member functions instead of the implicit conversion.
To always use a dot as decimal separator regardless of the locale, use [`to-string-unlocalized()`](../../property-types/numeric-types/#to-string-unlocalized---string).

For the other direction, convert a `string` to a number with the [`to-float()`](../../property-types/strings/#to-float---float) member function,
and check whether a string holds a valid number with `is-float()`.

All these conversions use the decimal separator of the current locale,
which is exposed as <Link type="Platform.decimal-separator" label="Platform.decimal-separator" /> and defaults to the dot (`.`).
For example, in a German locale the decimal separator is the comma (`,`):

-   The `float` value `1.5` converts to the string `"1,5"`.
-   `"1,5".to-float()` returns `1.5`.
-   `"1.5".is-float()` returns `false` and `"1.5".to-float()` returns `0`:
    only the locale's decimal separator is recognized when parsing.

Number literals in `.slint` files always use the dot, regardless of the locale.
See the <Link type="translations" label="translations guide"/> for how the decimal separator is determined.
