@@ -26,6 +26,7 @@ class Units extends CalculateAnything implements CalculatorInterface
26
26
private $ stop_words ;
27
27
private $ keywords ;
28
28
private $ unitsList ;
29
+ private $ fractionUnits ;
29
30
private $ lang ;
30
31
private $ match_units ;
31
32
@@ -39,6 +40,7 @@ public function __construct($query)
39
40
$ this ->keywords = $ this ->getKeywords ('units ' );
40
41
$ this ->stop_words = $ this ->getStopWords ('units ' );
41
42
$ this ->unitsList = $ this ->availableUnits ();
43
+ $ this ->fractionUnits = $ this ->fractionUnits ();
42
44
}
43
45
44
46
/**
@@ -170,6 +172,33 @@ private function availableUnits()
170
172
];
171
173
}
172
174
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
+
173
202
174
203
/**
175
204
* shouldProcess
@@ -250,6 +279,26 @@ public function output($result)
250
279
],
251
280
]
252
281
];
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
+ }
253
302
254
303
return $ items ;
255
304
}
@@ -295,6 +344,11 @@ public function convert($data)
295
344
return $ conversion_error ;
296
345
}
297
346
}
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
+ }
298
352
299
353
$ decimals = -1 ;
300
354
if ($ from_type == 'temperature ' ) {
@@ -333,7 +387,11 @@ public function convert($data)
333
387
334
388
return [
335
389
'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 )
337
395
];
338
396
}
339
397
0 commit comments