Fix amounts with "e" in them.

This commit is contained in:
James Cole
2022-03-26 18:13:02 +01:00
parent bb57015004
commit 8002610234

View File

@@ -473,6 +473,8 @@ class Steam
if ('' === $amount) {
return '0';
}
$amount = $this->floatalize($amount);
if (1 === bccomp($amount, '0')) {
$amount = bcmul($amount, '-1');
}
@@ -480,6 +482,34 @@ class Steam
return $amount;
}
/**
* https://framework.zend.com/downloads/archives
*
* Convert a scientific notation to float
* Additionally fixed a problem with PHP <= 5.2.x with big integers
*
* @param string $value
*/
public function floatalize(string $value): string
{
Log::debug(sprintf('floatalize("%s")', $value));
$value = strtoupper($value);
if (!str_contains($value, 'E')) {
return $value;
}
$number = substr($value, 0, strpos($value, 'E'));
if (str_contains($number, '.')) {
$post = strlen(substr($number, strpos($number, '.') + 1));
$mantis = substr($value, strpos($value, 'E') + 1);
if ($mantis < 0) {
$post += abs((int) $mantis);
}
return number_format((float)$value, $post, '.', '');
}
return number_format((float)$value, 0, '.', '');
}
/**
* @param string|null $amount
*