Skip to content

Commit c5bf500

Browse files
committed
add format to Decimal type
extend the unit tests to cover new 'format'-setting
1 parent 63b9cad commit c5bf500

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: _test/types/DecimalTest.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function test_validate_success($value, $min, $max, $decpoint = '.')
102102
public function valueProvider()
103103
{
104104
return [
105-
// $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix='', $engineering = false
105+
// $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix='', $engineering = false, $format = ''
106106
['5000', '5 000,00', '2', ',', ' ', false],
107107
['5000', '5 000', '2', ',', ' ', true],
108108
['5000', '5 000', '0', ',', ' ', false],
@@ -149,6 +149,18 @@ public function valueProvider()
149149
['1e15', '1000' . "\xE2\x80\xAF" . 'T', '-1', ',', ' ', true, '', '', true],
150150
['1e-21', '0.001' . "\xE2\x80\xAF" . 'a', '-1', ',', ' ', true, '', '', true],
151151

152+
//format string
153+
['5000', '5 000', '-1', '.', ' ', true, '', '', false, ''],
154+
['5000', '5 000.00', '2', '.', ' ', false, '', '', false, '%s'],
155+
['1.7', '1.70', '-1', '.', ' ', true, '', '', false, '%01.2f'],
156+
['1.7', '1.70' , '-1', '.', ' ', true, '', '', false, '%01.2F'],
157+
['1.7', '0001' , '-1', '.', ' ', true, '', '', false, "%'.04d"],
158+
['15', '1111' , '-1', '.', ' ', true, '', '', false, '%04b'],
159+
['362525200', '3.625e+8' , '-1', '.', ' ', true, '', '', false, '%.3e'],
160+
['362525200', '3.625E+8' , '-1', '.', ' ', true, '', '', false, '%.3E'],
161+
['1', '1' , '-1', '.', ' ', true, '', '', false, '%u'],
162+
['-1', '18446744073709551615' , '-1', '.', ' ', true, '', '', false, '%u'],
163+
152164
];
153165
}
154166

@@ -158,7 +170,8 @@ public function valueProvider()
158170
public function test_renderValue(
159171
$value, $expect, $roundto, $decpoint,
160172
$thousands, $trimzeros,
161-
$prefix = '', $postfix = '', $engineering = false
173+
$prefix = '', $postfix = '', $engineering = false,
174+
$format = ''
162175
)
163176
{
164177
$decimal = new Decimal([
@@ -168,7 +181,8 @@ public function test_renderValue(
168181
'trimzeros' => $trimzeros,
169182
'prefix' => $prefix,
170183
'postfix' => $postfix,
171-
'engineering' => $engineering
184+
'engineering' => $engineering,
185+
'format' => $format
172186
]);
173187
$R = new \Doku_Renderer_xhtml();
174188
$R->doc = '';

Diff for: types/Decimal.php

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Decimal extends AbstractMultiBaseType
2525
'prefix' => '',
2626
'postfix' => '',
2727
'engineering' => false,
28+
'format' => '',
2829
];
2930

3031
/**
@@ -37,6 +38,10 @@ class Decimal extends AbstractMultiBaseType
3738
*/
3839
public function renderValue($value, \Doku_Renderer $R, $mode)
3940
{
41+
if (preg_match("/^%(?:['+-:.]?\D?\d*\.?\d*)?[bdeEfFu]$/", $this->config['format'])) {
42+
$R->cdata($this->config['prefix'] . sprintf($this->config['format'], $value) . $this->config['postfix']);
43+
return true;
44+
}
4045

4146
if ($this->config['engineering']) {
4247
$unitsh = ['', 'k', 'M', 'G', 'T'];

0 commit comments

Comments
 (0)