我想在我的数据库中输入用户测量值,但我需要先将其格式化,但结果字符串似乎是空的这是我到目前为止所拥有的..
$data = $_POST['mesurement'];
$users_mesurement;
//if data has .
if (strpos($data,'.') !== false) {
//explode data
$testA = explode(".",$data);
// if testA[0] str is more than 2
if (strlen($testA[0]) > 2){
// if testA[1] str is below 1
if (strlen($testA[1]) < 2){
$users_mesurement = $data . "0";
// if testA[1] str is above 1
}else{
$users_mesurement = $data;
}
// has . but below 2
}else{
// if testA[1] str is below 1
if (strlen($testA[1]) < 2){
$users_mesurement = " " . $data . "0";
// if testA[1] str is above 1
}else{
$users_mesurement = " " . $data;
}
//if does not have .
}else{
//if is more than 2
if (strlen($data) > 2){
$users_mesurement = $data . ".00";
//if less than 2
}else{
$users_mesurement = " " . $data . ".00";
}
}
可能的$数据类型是
NNN.NN
NNN.N
NNN
NN.NN
NN.N
NN
N =任意数字
$ users_mesurement结果必须是&#34; NNN.NN&#34;或者&#34; NN.NN&#34;
好的,我现在有了这个
//if data has .
if (strpos($data,'.') !== false) {
//explode data
$testA = explode(".",$data);
$part1 = sprintf("%03d", $testA[0]);
$part2 = sprintf("%02d", $testA[1]);
$users_mesurement = $part1 . "." . $part2 . " cm";
//if does not have .
}else{
$part1 = sprintf("%03d", $data);
$users_mesurement = $part1 . "." . "00" . " cm";
}
并且当我输入99.5时它除了一部分它起作用它输出099.05我需要它是099.50
$part2 = str_pad($testA[1], 2 , "0");
现在这一切都很好了