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?

有帮助吗?

解决方案

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

Example

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top