Question

I have a string such as this:

10,14.5,3.6,10.4,2

I'd like to round down each number so the result would be this:

10,14,3,10,2

I'm looking for the cleanest and fastest solution? My first thought was to explode them into an array and run a loop and floor each value, but that seems a little clunky to me.

I also thought that maybe using regex to just remove everything after a decimal for each value, but I don't know how to do that or if it's more efficient?

Anyone have any suggestions?

Was it helpful?

Solution

$array = explode(",",'10,14.5,3.6,10.4,2');
$string = implode(",",array_map('floor',$array));

Example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top