PHP函数 - 用.00清理数字(PHP Function - cleaning up numbers with .00)
我有一个快速的问题。 我有这样的功能:
function clean_num($num){
return trim(trim($num, '0'), '.');
}
所以当我有85.00和55.00这样的数字时,它就会起飞.00并且只返回85和55 。
那么我有一个0.00的数字,它会返回空白。 它应该只是说0,但我没有得到任何结果。
我如何修改这个函数来获得我正在寻找的东西?
谢谢!
I have a quick question. I have a function like this:
function clean_num($num){
return trim(trim($num, '0'), '.');
}
So when I have numbers like 85.00 and 55.00 it takes off the .00 and just returns 85 and 55.
Well I have a number 0.00 and what happens is it is returning blank. It should just say 0 but I am not getting a result at all.
How can I modify this function to get what I am looking for?
Thanks!
原文:https://stackoverflow.com/questions/7842694
更新时间:2019-11-26 08:10
最满意答案
trim()适用于两端。 要仅修剪右端,请使用rtrim() 。
function clean_num($num){
return rtrim(rtrim($num, '0'), '.');
}
trim() works on both ends. To only trim on the right end, use rtrim().
function clean_num($num){
return rtrim(rtrim($num, '0'), '.');
}
2011-10-20
相关问答
你的架构开始是错误的。 问题不在于设置日期本身,这显然是无效的,MySQL和PHP都拒绝它,因为没有0年和0月没有0,你看到的输出只是对一种有效的日期(1年1月1日和1月1日之前的1天)。 但是如果你做得对的话,你也只是错过了Doctrine摘录它的观点: $entEmail = new Email();
$entEmail->setViewedAt(null);
Doctrine现在很乐意将NULL放在数据库列中。 Your architecture is wrong to begin wit
...
trim()适用于两端。 要仅修剪右端,请使用rtrim() 。 function clean_num($num){
return rtrim(rtrim($num, '0'), '.');
}
trim() works on both ends. To only trim on the right end, use rtrim(). function clean_num($num){
return rtrim(rtrim($num, '0'), '.');
}
尝试这个: $that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
看演示 Try this: $that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
您收到的日期采用MySQL无法识别的格式。 您必须先将其转换为可接受的日期和时间字面值 ,然后才能将其传递给INSERT。 这种转换可以通过两种方式完成: 您可以配置JS DateTime Picker,使其返回“YYYY-MM-DD HH:MM:SS”的日期。 您可以在PHP中执行上述转换。 无论你选择哪个,坚持保存到数据库的日期的年 - 月 - 日格式,这样你就不会发现为什么“08/02”被认为是8月2日而不是2月8日。 The date you receive is in a format
...
您可以使用new DateTime() ! 希望这会很好 You may use new DateTime() ! Hope this 'll hep
$hour = 0;
while($hour++ < 24)
{
$timetoprint = date('G:i:s',mktime($hour,0,0,1,1,2011));
echo $timetoprint;
}
?>
那会像你想要的那样去做吗? 我假设您需要将日期变量放在该表单中是有原因的。 否则,评论是要走的路。 <?php
$hour = 0;
while($hour++ < 24)
{
$timetoprint = date('G:i:s',m
...
将 $price = substr($price, -3) == ".00" ? substr($price, 0, -3) : $price;
工作? Would $price = substr($price, -3) == ".00" ? substr($price, 0, -3) : $price;
work?
使用Number_format函数 echo number_format($number,2,',',' ');
Use Number_format function echo number_format($number,2,',',' ');
使用DateTime :: createFromFormat : $date = DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;
您的代码无法正常工作,因为strtotime 根据实际格式的分隔符进行假设: m/d/Y - 美式格式 dmY或dmY - 欧洲 Use DateTime::createFromForma
...