Skip to content

Commit 51dec36

Browse files
committed
add eng notation
1 parent bbfcbfb commit 51dec36

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

types/Decimal.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Decimal extends AbstractMultiBaseType
2323
'thousands' => "\xE2\x80\xAF", // narrow no-break space
2424
'trimzeros' => true,
2525
'prefix' => '',
26-
'postfix' => ''
26+
'postfix' => '',
27+
'engineering' => false
2728
);
2829

2930
/**
@@ -36,6 +37,27 @@ class Decimal extends AbstractMultiBaseType
3637
*/
3738
public function renderValue($value, \Doku_Renderer $R, $mode)
3839
{
40+
41+
if ($this->config['engineering']) {
42+
$unitsh = array('', 'k', 'M', 'G', 'T');
43+
$unitsl = array('', 'm', 'µ', 'n', 'p', 'f', 'a');
44+
45+
$exp = floor(log10($value)/3);
46+
47+
if ($exp < 0) {
48+
$units = $unitsl;
49+
$mul = -1;
50+
} else {
51+
$units = $unitsh;
52+
$mul = 1;
53+
}
54+
55+
56+
$R->cdata($this->config['prefix'] . $value / 10**($exp*3) . "\xE2\x80\xAF" . $units[$exp*$mul] . $this->config['postfix'] );
57+
return true;
58+
}
59+
60+
3961
if ($this->config['roundto'] == -1) {
4062
$value = $this->formatWithoutRounding(
4163
$value,
@@ -56,7 +78,8 @@ public function renderValue($value, \Doku_Renderer $R, $mode)
5678
$value = rtrim($value, $this->config['decpoint']);
5779
}
5880

59-
$R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
81+
82+
$R->cdata($this->config['prefix'] . $value . $this->config['postfix'] );
6083
return true;
6184
}
6285

0 commit comments

Comments
 (0)