[Type] Coerce float to string
Currently, it is not possible to convert a float -> string. https://github.com/azjezz/psl/blob/c34f75bcb27298a1fdb017c07cd90f07c78046e2/src/Psl/Type/Internal/StringType.php#L39
The other way around (string -> float) is possible.
We could go for one of these solutions to make it possible in a locale-safe way:
- https://github.com/moneyphp/money/blob/512333ef834bd77f76e865a30eb5963d480e2ed7/src/Number.php#L64-L71
- https://github.com/brick/math/blob/49d9d6d83d9f97ebc6e8f3d7a4adbc7cd649afc2/src/BigNumber.php#L165-L175
Would it make sense to add it and do you see any additional risks?
I think this got fixed in PHP 8.0 right? There casting a float to string is no longer locale aware.
Yes it did: https://wiki.php.net/rfc/locale_independent_float_to_string
But PSL1 also supports PHP 74.
So it's probably best to go for something like
sprintf('%.14F', $number)
But that means we need to specify a precision - which can be seen as a data manipulation. Because it results in:
>>> sprintf('%.14F', 3.132)
=> "3.13200000000000"
We could rtrim 0 from the fractional part or leave it as-is : it doesn't hurt anyone as long as the number is >= max([0, ini_get('precision')])
we are talking about string -> float here, not float -> string, right?
Sorry for the confusion. I updated the issue. It's about coercing a float -> string. See https://github.com/azjezz/psl/blob/c34f75bcb27298a1fdb017c07cd90f07c78046e2/src/Psl/Type/Internal/StringType.php#L39
@veewee are you interested in writing a PR for this?
i guess we can go with something like:
$str = Str\trim_right(Str\format('%.14F', $value), '0');
if (Str\ends_with($str, '.')) {
$str .= '0';
}
return $str;
see https://github.com/azjezz/psl/blob/1.5.x/src/Psl/Type/Internal/LiteralScalarType.php#L135-L143
Sure, I'll give it a try later this week.
I moved around this by using numeric_string()->coerce($float) instead.
Seems sufficient to me.