Open Enyium opened 3 months ago
Thanks for filing an issue.
number.to-fixed(xx)
and number.to-precision(xx)
@tr
macro, but that is a different issue.As a workaround, toPrecision can be emulated with something like round(foo * 100) / 100
but it's just a workaround.
As another workaround, it is possible to implement in native code in a global callback.
Added as good first issue now that the number.sqrt()
and similar are merged, it can just be done similarly.
What needs to be done is:
I was also looking for a convenient solution to convert floats to a string on the UI with a fixed number of decimals and potentially a minimum width.
I looked into the slint code Olivier pointed to and attempted to implement a solution utilizing std::fmt
.
std::fmt
's concept of precision is this:
For floating-point types, this indicates how many digits after the decimal point should be printed.
So specifying the digits to appear after the decimal points (like with toFixed()
) can easily be done with std::fmt
but specifying the number of significant digits (like with toPrecision()
) is not supported out-of-the-box with std::fmt
IMO.
My main interest was in providing a proposal for setting the number of digits after the decimal point and for setting the width. The code can be found here
@marcothaller Thanks. I added a few comment to the commit.
On a unrelated note, i wonder if the function should follow the locale or not.
It's a basic UI necessity to display
float
s in a controlled format. Currently, you can only let yourfloat
be displayed with inappropriate accuracy (like5.9993034
in stead of6.0
) or implement it yourself. Trying to round yourself in thefloat
domain, then accepting implicit conversion tostring
can lead to base-2-related problems like3.299999999999
or omission of an intended.0
suffix. Slint should provide such functionality. It could copy JavaScript'stoFixed()
and maybe alsotoPrecision()
, which return strings.Ideally, Slint would be aware of the current locale (from the translation context?) and use the appropriate decimal separator - also with regular auto-conversions.