Skip to content

Commit

Permalink
Ensure local-independent float to string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
into-the-v0id committed Dec 19, 2023
1 parent 463b220 commit 0f7c9ad
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Helper/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
namespace IntoTheVoid\Env\Helper;

use function explode;
use function ini_get;
use function is_float;
use function is_string;
use function mb_strlen;
use function mb_strpos;
use function mb_substr;
use function preg_match;
use function sprintf;
use function substr;

/**
* @internal
Expand Down Expand Up @@ -110,10 +114,21 @@ public static function from($value): string
}

if (is_float($value)) {
$value = (string) $value;
$precision = ini_get('precision');
if ($precision === false || $precision === '' || preg_match('/^[0-9]+$/', $precision) !== 1) {
$precision = '14';
}

$value = sprintf('%.' . $precision . 'F', $value);
while (self::endsWith($value, '0') && ! self::endsWith($value, '.0')) {
$value = substr($value, 0, -1);
}

if (! self::contains($value, '.')) {
$value .= '.0';
}

return $value;
}

return (string) $value;
Expand Down

0 comments on commit 0f7c9ad

Please sign in to comment.