Skip to content

Commit 97fe718

Browse files
[11.x] Method to trim '0' digits after decimal point of a given number (#52284)
* Number::json() method * Rename method * Update Number.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4d6ff33 commit 97fe718

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Illuminate/Support/Number.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ public static function pairs(int|float $to, int|float $by, int|float $offset = 1
257257
return $output;
258258
}
259259

260+
/**
261+
* Remove any trailing zero digits after the decimal point of the given number.
262+
*
263+
* @param int|float $number
264+
* @return int|float
265+
*/
266+
public static function trim(int|float $number)
267+
{
268+
return json_decode(json_encode($number));
269+
}
270+
260271
/**
261272
* Execute the given callback using the given locale.
262273
*

tests/Support/SupportNumberTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,15 @@ public function testPairs()
293293
$this->assertSame([[0, 10], [10, 20], [20, 25]], Number::pairs(25, 10, 0));
294294
$this->assertSame([[0, 2.5], [2.5, 5.0], [5.0, 7.5], [7.5, 10.0]], Number::pairs(10, 2.5, 0));
295295
}
296+
297+
public function testTrim()
298+
{
299+
$this->assertSame(12, Number::trim(12));
300+
$this->assertSame(120, Number::trim(120));
301+
$this->assertSame(12, Number::trim(12.0));
302+
$this->assertSame(12.3, Number::trim(12.3));
303+
$this->assertSame(12.3, Number::trim(12.30));
304+
$this->assertSame(12.3456789, Number::trim(12.3456789));
305+
$this->assertSame(12.3456789, Number::trim(12.34567890000));
306+
}
296307
}

0 commit comments

Comments
 (0)