質問

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