mirror of
https://github.com/kevinpapst/kimai2.git
synced 2025-03-29 19:11:32 +00:00
make sure decimal duration is always rendered with two decimals
This commit is contained in:
parent
753c10df5c
commit
c61fea2612
2 changed files with 21 additions and 5 deletions
|
@ -24,6 +24,10 @@ final class LocaleHelper
|
|||
* @var NumberFormatter
|
||||
*/
|
||||
private $numberFormatter;
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
private $durationFormatter;
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
|
@ -51,10 +55,12 @@ final class LocaleHelper
|
|||
$value = 0;
|
||||
}
|
||||
|
||||
return $this->getNumberFormatter()->format((float) $value);
|
||||
return $this->getDurationFormatter()->format((float) $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only used in twig filter |amount and invoice templates
|
||||
*
|
||||
* @param string|float $amount
|
||||
* @return bool|false|string
|
||||
*/
|
||||
|
@ -133,6 +139,16 @@ final class LocaleHelper
|
|||
return $this->numberFormatter;
|
||||
}
|
||||
|
||||
private function getDurationFormatter(): NumberFormatter
|
||||
{
|
||||
if (null === $this->numberFormatter) {
|
||||
$this->durationFormatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL);
|
||||
$this->durationFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
|
||||
}
|
||||
|
||||
return $this->durationFormatter;
|
||||
}
|
||||
|
||||
private function getMoneyFormatter(bool $withCurrency = true): NumberFormatter
|
||||
{
|
||||
if ($withCurrency) {
|
||||
|
|
|
@ -199,18 +199,18 @@ class LocaleHelperTest extends TestCase
|
|||
$sut = $this->getSut('de');
|
||||
$this->assertEquals('2,62', $sut->durationDecimal($record->getDuration()));
|
||||
$this->assertEquals('6.328,89', $sut->durationDecimal(22784012));
|
||||
$this->assertEquals('1', $sut->durationDecimal(3600));
|
||||
$this->assertEquals('1,00', $sut->durationDecimal(3600));
|
||||
$this->assertEquals('1,01', $sut->durationDecimal(3630));
|
||||
$this->assertEquals('1,02', $sut->durationDecimal(3661));
|
||||
$this->assertEquals('1,1', $sut->durationDecimal(3960));
|
||||
$this->assertEquals('1,10', $sut->durationDecimal(3960));
|
||||
|
||||
// test negative duration
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('0', $sut->durationDecimal(-1));
|
||||
$this->assertEquals('0.00', $sut->durationDecimal(-1));
|
||||
|
||||
// test zero duration
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('0', $sut->durationDecimal(0));
|
||||
$this->assertEquals('0.00', $sut->durationDecimal(0));
|
||||
$this->assertEquals('6,328.89', $sut->durationDecimal(22784012));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue