add eng notation

This commit is contained in:
Philipp Cochems
2022-09-13 23:10:21 +02:00
parent bbfcbfb5a5
commit 51dec36840

View File

@ -23,7 +23,8 @@ class Decimal extends AbstractMultiBaseType
'thousands' => "\xE2\x80\xAF", // narrow no-break space
'trimzeros' => true,
'prefix' => '',
'postfix' => ''
'postfix' => '',
'engineering' => false
);
/**
@ -36,6 +37,27 @@ class Decimal extends AbstractMultiBaseType
*/
public function renderValue($value, \Doku_Renderer $R, $mode)
{
if ($this->config['engineering']) {
$unitsh = array('', 'k', 'M', 'G', 'T');
$unitsl = array('', 'm', 'µ', 'n', 'p', 'f', 'a');
$exp = floor(log10($value)/3);
if ($exp < 0) {
$units = $unitsl;
$mul = -1;
} else {
$units = $unitsh;
$mul = 1;
}
$R->cdata($this->config['prefix'] . $value / 10**($exp*3) . "\xE2\x80\xAF" . $units[$exp*$mul] . $this->config['postfix'] );
return true;
}
if ($this->config['roundto'] == -1) {
$value = $this->formatWithoutRounding(
$value,
@ -56,7 +78,8 @@ class Decimal extends AbstractMultiBaseType
$value = rtrim($value, $this->config['decpoint']);
}
$R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
$R->cdata($this->config['prefix'] . $value . $this->config['postfix'] );
return true;
}