[英]How to remove leading zeroes from float value in php
Hello 你好
I am trying to remove leading zeroes from my float value but cannot do it. 我正在尝试从浮点值中删除前导零,但不能这样做。
I have a number 012.36
which I need to convert as 12.36
. 我有一个数字012.36
,我需要将其转换为12.36
。 I have used ltrim
to remove leading zeroes but when I use ltrim
the variable is changed to string and I cannot use it as a number. 我已经使用ltrim
删除了前导零,但是当我使用ltrim
,变量已更改为字符串,并且不能将其用作数字。
When I again try to convert it to float value using (float)
or floatval
it again adds a leading zero to the value like 012.36
当我再次尝试使用(float)
或floatval
将其转换为浮点值时,它再次向值012.36
添加前导零
I know it is a simple problem but can't get to solve it. 我知道这是一个简单的问题,但无法解决。 Please help.. 请帮忙..
Here is the code. 这是代码。 I have a function which returns me the value and I have used it in a variable. 我有一个返回值的函数,并且已在变量中使用它。
Here is my Function 这是我的职能
public function getCartPercentShippingRate(){
$shippingRateCollection = $this->getShippingMethodForShippingCountry();
$shippingRate = 0;
foreach ($shippingRateCollection as $shipping){
$shippingRate = $shipping->getShippingRate();
}
$quote=Mage::getSingleton('checkout/session')->getQuote();
$totals = $quote->getSubtotal();
$shippingPercent = $totals * $shippingRate/100;
return $shippingPercent;
}
$price = $shippingRateModel->getCartPercentShippingRate();
When I echo the variable using echo $price
it displays 012.36
. 当我使用echo $price
回显变量时,它显示012.36
。
When I use echo $price = (float)($price);
当我使用echo $price = (float)($price);
It still displays 012.36
. 它仍然显示012.36
。
I have tried this. 我曾经尝试这样做。 $newprice = ltrim($price, '0');
It displays the $newprice
as 12.36
. 它将$newprice
显示为12.36
。 But when I use it in my another function to set price like this, $method->setPrice($newprice);
但是当我在另一个函数中使用它来设置价格时, $method->setPrice($newprice);
It displays me 0 as the new price. 它向我显示0作为新价格。
If I use static value like $method->setPrice(12.36);
如果我使用静态值,例如$method->setPrice(12.36);
Is displays the new price as 12.36. 将显示新价格为12.36。
Try this may it'll help you... First convert value into string and do this. 尝试此操作可能会对您有所帮助...首先将值转换为字符串并执行此操作。
$str = ltrim($str, '0');
Example 例
$val = 012.36;
$val = ltrim($val, '0');
echo (float) $val;
I am not sure how you have tried it. 我不确定您如何尝试过。 Following code gives me what want 以下代码给我想要的
$num = 012.36;
echo (float)$num; // outputs 12.36
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.