Question

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. 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.
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
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.

When I use echo $price = (float)($price); It still displays 012.36.
I have tried this. $newprice = ltrim($price, '0'); It displays the $newprice as 12.36. But when I use it in my another function to set price like this, $method->setPrice($newprice); It displays me 0 as the new price.
If I use static value like $method->setPrice(12.36); Is displays the new price as 12.36.

Was it helpful?

Solution 2

use (float) 

Here is the demo

DEMO

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top