Skip to content

Commit d20554a

Browse files
Merge pull request #95 from arvidbjorkstrom/fractional_sub_units
Fractional sub-units
2 parents f4c8d63 + 5cf1f40 commit d20554a

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

workflow/tools/units.php

+59-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Units extends CalculateAnything implements CalculatorInterface
2626
private $stop_words;
2727
private $keywords;
2828
private $unitsList;
29+
private $fractionUnits;
2930
private $lang;
3031
private $match_units;
3132

@@ -39,6 +40,7 @@ public function __construct($query)
3940
$this->keywords = $this->getKeywords('units');
4041
$this->stop_words = $this->getStopWords('units');
4142
$this->unitsList = $this->availableUnits();
43+
$this->fractionUnits = $this->fractionUnits();
4244
}
4345

4446
/**
@@ -170,6 +172,33 @@ private function availableUnits()
170172
];
171173
}
172174

175+
private function fractionUnits() {
176+
return [
177+
'm' => 'cm',
178+
'km' => 'm',
179+
'dm' => 'cm',
180+
'cm' => 'mm',
181+
'mm' => 'μm',
182+
'μm' => 'nm',
183+
'nm' => 'pm',
184+
'ft' => 'in',
185+
'yd' => 'in',
186+
'mi' => 'ft',
187+
'l' => 'ml',
188+
'pt' => 'floz',
189+
'uspt' => 'floz',
190+
'ukpt' => 'floz',
191+
'gal' => 'qt',
192+
'qt' => 'pt',
193+
'usqt' => 'uspt',
194+
'ukqt' => 'ukpt',
195+
'usgal' => 'usqt',
196+
'ukgal' => 'ukqt',
197+
'g' => 'mg',
198+
'st' => 'lb',
199+
];
200+
}
201+
173202

174203
/**
175204
* shouldProcess
@@ -250,6 +279,26 @@ public function output($result)
250279
],
251280
]
252281
];
282+
if(isset($result['fraction']) and $result['fraction']) {
283+
$items[] = [
284+
'title' => $result['fraction']['formatted'],
285+
'arg' => $result['fraction']['value'],
286+
'subtitle' => $this->getText('action_copy'),
287+
'valid' => true,
288+
'mods' => [
289+
'cmd' => [
290+
'valid' => true,
291+
'arg' => $this->cleanupNumber($result['fraction']['value']),
292+
'subtitle' => $this->lang['cmd'],
293+
],
294+
'alt' => [
295+
'valid' => true,
296+
'arg' => $result['fraction']['formatted'],
297+
'subtitle' => $this->lang['alt'],
298+
],
299+
]
300+
];
301+
}
253302

254303
return $items;
255304
}
@@ -295,6 +344,11 @@ public function convert($data)
295344
return $conversion_error;
296345
}
297346
}
347+
if(in_array($to, $this->fractionUnits) and fmod($converted,1) > 0) {
348+
$convert->from(fmod($converted,1),$to);
349+
$fraction = $convert->to($this->fractionUnits[$to]);
350+
$fraction_unit = $this->fractionUnits[$to];
351+
}
298352

299353
$decimals = -1;
300354
if ($from_type == 'temperature') {
@@ -333,7 +387,11 @@ public function convert($data)
333387

334388
return [
335389
'formatted' => $resultValue .' ' . $resultUnit,
336-
'value' => $resultValue
390+
'value' => $resultValue,
391+
'fraction' => (isset($fraction)?[
392+
'formatted' => bcdiv($converted, 1, 0).' '.$resultUnit.', '.$fraction.' '.$fraction_unit,
393+
'value' => $resultValue
394+
]:false)
337395
];
338396
}
339397

0 commit comments

Comments
 (0)