PHP 分隔字符串 [英] PHP separate string
问题描述
我有一个包含 128.82
的字符串.我想在 100
和 28.82
中拆分/分离它们.
I have a string that contains 128.82
. I want to split/separate them in 100
and 28.82
.
var_dump(substr($kwh,0,3));
die();
它给了我 128
.
如何将 100
和 28.82
分开?
How can I separate the 100
and 28.82
?
任何帮助将不胜感激
注意:我设置这个是因为我已经定义了slab.1-100
、101-150
等等.所以我需要根据板来设置它们.平板可能会有所不同,因为它可能是 1-50
、51-100
、100-150
等等.所以我必须划分/拆分 128.82
就像 50
用于 1-50
,50
用于 51-100
然后是 28.82
对于 101-150
Note: I am setting this because I have defined slabs. 1-100
, 101-150
, and so on. So I need to set them according to the slabs. The slabs may differ as it could be 1-50
, 51-100
, 100-150
, and so on. so I have to divide/split 128.82
like 50
for 1-50
, 50
for 51-100
and then 28.82
for 101-150
推荐答案
如果要将 128.82
拆分为 100
和 28.82
你可以这样做:
If you want to split 128.82
into 100
and 28.82
You can do it like this:
<?php
function calcSlabSize($size) {
$slab = floor($size / 50)*50;
return [$slab,$size-$slab];
}
print_r( calcSlabSize(128.82));
这将导致:
Array ( [0] => 100 [1] => 28.82 )
这篇关于PHP 分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!