Improve amount conversion code.

This commit is contained in:
James Cole
2018-07-01 13:34:57 +02:00
parent 8e38f5c2c0
commit e2ec9ca5fb
4 changed files with 165 additions and 112 deletions

View File

@@ -98,8 +98,31 @@ class Amount implements ConverterInterface
$value = str_replace($search, '', $value);
Log::debug(sprintf('No decimal character found. Converted amount from "%s" to "%s".', $original, $value));
}
if ($value{0} === '.') {
$value = '0' . $value;
}
return number_format(round((float)$value, 12), 12, '.', '');
if (is_numeric($value)) {
Log::debug(sprintf('Final NUMERIC value is: "%s"', $value));
return $value;
}
Log::debug(sprintf('Final value is: "%s"', $value));
$formatted = sprintf('%01.12f', $value);
Log::debug(sprintf('Is formatted to : "%s"', $formatted));
return $formatted;
}
private function bcround($number, $scale = 0)
{
$fix = "5";
for ($i = 0; $i < $scale; $i++) {
$fix = "0$fix";
}
$number = bcadd($number, "0.$fix", $scale + 1);
return bcdiv($number, "1.0", $scale);
}
/**