Skip to content

Commit 765d0d5

Browse files
authored
Merge pull request #617 from PhilippCo/eng_notation
Eng notation
2 parents 0062407 + df2cde2 commit 765d0d5

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

_test/Type_Decimal.test.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function test_validate_success($value, $min, $max, $decpoint = '.')
101101
public function valueProvider()
102102
{
103103
return array(
104-
// $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix=''
104+
// $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix='', $engineering = false
105105
array('5000', '5 000,00', '2', ',', ' ', false),
106106
array('5000', '5 000', '2', ',', ' ', true),
107107
array('5000', '5 000', '0', ',', ' ', false),
@@ -117,21 +117,46 @@ public function valueProvider()
117117

118118
array('-0.55600', '$ -0,556', '4', ',', ' ', true, '$ '),
119119
array('-0.55600', '-0,556 EUR', '4', ',', ' ', true, '', ' EUR'),
120+
121+
//engineering notation
122+
array('1e-18', '1'."\xE2\x80\xAF".'a', '-1', ',', ' ', true, '', '', true),
123+
array('1e-15', '1'."\xE2\x80\xAF".'f', '-1', ',', ' ', true, '', '', true),
124+
array('1e-12', '1'."\xE2\x80\xAF".'p', '-1', ',', ' ', true, '', '', true),
125+
array('1e-9', '1'."\xE2\x80\xAF".'n', '-1', ',', ' ', true, '', '', true),
126+
array('1e-6', '1'."\xE2\x80\xAF".'µ', '-1', ',', ' ', true, '', '', true),
127+
array('1e-3', '1'."\xE2\x80\xAF".'m', '-1', ',', ' ', true, '', '', true),
128+
129+
array('1e3', '1'."\xE2\x80\xAF".'k', '-1', ',', ' ', true, '', '', true),
130+
array('1e6', '1'."\xE2\x80\xAF".'M', '-1', ',', ' ', true, '', '', true),
131+
array('1e9', '1'."\xE2\x80\xAF".'G', '-1', ',', ' ', true, '', '', true),
132+
array('1e12', '1'."\xE2\x80\xAF".'T', '-1', ',', ' ', true, '', '', true),
133+
134+
array('1e4', '10'. "\xE2\x80\xAF".'k', '-1', ',', ' ', true, '', '', true),
135+
array('1e5', '100'."\xE2\x80\xAF".'k', '-1', ',', ' ', true, '', '', true),
136+
137+
array('1e-4', '100'."\xE2\x80\xAF".'µ', '-1', ',', ' ', true, '', '', true),
138+
array('1e-5', '10'. "\xE2\x80\xAF".'µ', '-1', ',', ' ', true, '', '', true),
139+
140+
//test behaviour if number exceeds prefix array
141+
array('1e15', '1000'. "\xE2\x80\xAF".'T', '-1', ',', ' ', true, '', '', true),
142+
array('1e-21', '0.001'."\xE2\x80\xAF".'a', '-1', ',', ' ', true, '', '', true),
143+
120144
);
121145
}
122146

123147
/**
124148
* @dataProvider valueProvider
125149
*/
126-
public function test_renderValue($value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix = '', $postfix = '')
150+
public function test_renderValue($value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix = '', $postfix = '', $engineering = false)
127151
{
128152
$decimal = new Decimal(array(
129153
'roundto' => $roundto,
130154
'decpoint' => $decpoint,
131155
'thousands' => $thousands,
132156
'trimzeros' => $trimzeros,
133157
'prefix' => $prefix,
134-
'postfix' => $postfix
158+
'postfix' => $postfix,
159+
'engineering' => $engineering
135160
));
136161
$R = new \Doku_Renderer_xhtml();
137162
$R->doc = '';

types/Decimal.php

Lines changed: 29 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,31 @@ 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+
$pfkey = -1 * $exp;
50+
} else {
51+
$units = $unitsh;
52+
$pfkey = $exp;
53+
}
54+
55+
if (count($units) <= ($pfkey+1)) { //check if number is within prefixes
56+
$pfkey = sizeof($units)-1;
57+
$exp = $pfkey * $exp/abs($exp);
58+
}
59+
60+
$R->cdata($this->config['prefix'] . $value / 10**($exp*3) . "\xE2\x80\xAF" . $units[$pfkey] . $this->config['postfix'] );
61+
return true;
62+
}
63+
64+
3965
if ($this->config['roundto'] == -1) {
4066
$value = $this->formatWithoutRounding(
4167
$value,
@@ -56,7 +82,8 @@ public function renderValue($value, \Doku_Renderer $R, $mode)
5682
$value = rtrim($value, $this->config['decpoint']);
5783
}
5884

59-
$R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
85+
86+
$R->cdata($this->config['prefix'] . $value . $this->config['postfix'] );
6087
return true;
6188
}
6289

0 commit comments

Comments
 (0)