Skip to content

enable Float to percentage conversions#10980

Draft
Murmele wants to merge 3 commits intoslint-ui:masterfrom
Murmele:mm/percentage_calculations
Draft

enable Float to percentage conversions#10980
Murmele wants to merge 3 commits intoslint-ui:masterfrom
Murmele:mm/percentage_calculations

Conversation

@Murmele
Copy link
Copy Markdown
Contributor

@Murmele Murmele commented Mar 9, 2026

Description: This allows to do calculations on relative values by using Floats and converting then to percentage by *1% or by dividing by 100. Example:

private property <float> f: 50;
private property <percent> p: (f / 2) * 1%;
private property <float> f: 50;
private property <percent> p: (f / 2) / 100;

Fixes: #3785

Description: This allows to do calculations on relative values by using Floats and converting then to percentage by *1% or by dividing by 100.
Example:
```slint
private property <float> f: 50;
private property <percent> p: (f / 2) * 1%;
```
Fixes: slint-ui#3785
@Murmele Murmele requested a review from ogoffart March 9, 2026 10:46
@ogoffart
Copy link
Copy Markdown
Member

We need to discuss this when I'm back from vacation. I'm a bit worried that the implicit conversion may cause problem for cases where the behaviour is different depending on Whether the type is a % or not (eg, colour functions or width/height.)

@Murmele
Copy link
Copy Markdown
Contributor Author

Murmele commented Mar 11, 2026

We need to discuss this when I'm back from vacation. I'm a bit worried that the implicit conversion may cause problem for cases where the behaviour is different depending on Whether the type is a % or not (eg, colour functions or width/height.)

Because of that I assigned to you. It is no rush to merge it :)
For length properties a unit must be specified additionally to the float value because there is no direct conversion from float to length

@ogoffart
Copy link
Copy Markdown
Member

Another reason i'm not in favor of this is that code like that:

private property <percent> p: 50;

It looks like it is 50%, but it is instead 5000%

This means that forgetting an unit no longer result in a compilation error but now gives the wrong result, which is against the principle of type safety.

Ideally we should have the following to work:

private property <float> f: 50;
private property <percent> p: (f / 2) * 1%;

But it doesn't, because the multiplication by 1% does't change the type and maybe it should.

In other word, we should instead change the type of an expression that multiplies by a percentage, rather than having an automatic conversion.

@Murmele Murmele marked this pull request as draft March 17, 2026 07:56
@Murmele
Copy link
Copy Markdown
Contributor Author

Murmele commented Mar 17, 2026

Another reason i'm not in favor of this is that code like that:

private property <percent> p: 50;

It looks like it is 50%, but it is instead 5000%

This means that forgetting an unit no longer result in a compilation error but now gives the wrong result, which is against the principle of type safety.

Ideally we should have the following to work:

private property <float> f: 50;
private property <percent> p: (f / 2) * 1%;

But it doesn't, because the multiplication by 1% does't change the type and maybe it should.

In other word, we should instead change the type of an expression that multiplies by a percentage, rather than having an automatic conversion.

This is a good point, I will create later some tests regarding those cases so we don't forget them. Do you think it would make sense to create an explicit function for it? Like float_to_percent() instead of multiplying by *1%

@tronical
Copy link
Copy Markdown
Member

I think some-float * 1% should not produce an error but create a percentage value, that would be consistent with the rest of the unit handling - as opposed to having a special function just for percent.

@Murmele
Copy link
Copy Markdown
Contributor Author

Murmele commented Mar 24, 2026

I think some-float * 1% should not produce an error but create a percentage value, that would be consistent with the rest of the unit handling - as opposed to having a special function just for percent.

Fine for me. I will check it out next days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Ability to convert from float/int to percent

3 participants