Skip to content

Commit fd32a8f

Browse files
committed
Added precision and rounding mode in FloatCast
1 parent d6e3f7b commit fd32a8f

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

system/DataCaster/Cast/FloatCast.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace CodeIgniter\DataCaster\Cast;
1515

16+
use CodeIgniter\DataCaster\Exceptions\CastException;
17+
1618
/**
1719
* Class FloatCast
1820
*
@@ -30,6 +32,31 @@ public static function get(
3032
self::invalidTypeValueError($value);
3133
}
3234

33-
return (float) $value;
35+
$precision = isset($params[0]) ? (int) $params[0] : null;
36+
37+
if ($precision === null) {
38+
return (float) $value;
39+
}
40+
41+
// Map string flags to PHP constants
42+
$modeMap = [
43+
'up' => PHP_ROUND_HALF_UP,
44+
'down' => PHP_ROUND_HALF_DOWN,
45+
'even' => PHP_ROUND_HALF_EVEN,
46+
'odd' => PHP_ROUND_HALF_ODD,
47+
];
48+
49+
$mode = PHP_ROUND_HALF_UP; // Default mode
50+
51+
if (isset($params[1])) {
52+
$modeParam = strtolower($params[1]);
53+
if (isset($modeMap[$modeParam])) {
54+
$mode = $modeMap[$modeParam];
55+
} else {
56+
throw CastException::forInvalidFloatRoundingMode($params[1]);
57+
}
58+
}
59+
60+
return round((float) $value, $precision, $mode);
3461
}
3562
}

system/Entity/Exceptions/CastException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,14 @@ public static function forInvalidEnumType(string $expectedClass, string $actualC
122122
{
123123
return new static(lang('Cast.enumInvalidType', [$actualClass, $expectedClass]));
124124
}
125+
126+
/**
127+
* Thrown when an invalid rounding mode is provided for float casting.
128+
*
129+
* @return static
130+
*/
131+
public static function forInvalidFloatRoundingMode(string $mode)
132+
{
133+
return new static(lang('Cast.floatInvalidRoundingMode', [$mode]));
134+
}
125135
}

tests/system/DataConverter/DataConverterTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,62 @@ public static function provideConvertDataFromDB(): iterable
197197
'temp' => 15.9,
198198
],
199199
],
200+
'float precise' => [
201+
[
202+
'id' => 'int',
203+
'temp' => 'float[2]',
204+
],
205+
[
206+
'id' => '1',
207+
'temp' => '15.98765',
208+
],
209+
[
210+
'id' => 1,
211+
'temp' => 15.99,
212+
]
213+
],
214+
'float precise-down' => [
215+
[
216+
'id' => 'int',
217+
'temp' => 'float[2,down]',
218+
],
219+
[
220+
'id' => '1',
221+
'temp' => '1.235',
222+
],
223+
[
224+
'id' => 1,
225+
'temp' => 1.23,
226+
]
227+
],
228+
'float precise-even' => [
229+
[
230+
'id' => 'int',
231+
'temp' => 'float[2,even]',
232+
],
233+
[
234+
'id' => '1',
235+
'temp' => '20.005',
236+
],
237+
[
238+
'id' => 1,
239+
'temp' => 20.00,
240+
]
241+
],
242+
'float precise-odd' => [
243+
[
244+
'id' => 'int',
245+
'temp' => 'float[2,odd]',
246+
],
247+
[
248+
'id' => '1',
249+
'temp' => '1.255',
250+
],
251+
[
252+
'id' => 1,
253+
'temp' => 1.25,
254+
]
255+
],
200256
'enum string-backed' => [
201257
[
202258
'id' => 'int',

0 commit comments

Comments
 (0)