Welcome Guest, Not a member yet? Register   Sign In
Shopping Cart and leading zeros issue
#4

Showing prices brings internationalization aspect to be solved. But if you are sure that you would work with dollars only, some quick helper functions could be applied.

File: price_helper.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

// Adapted for dollar currency

function price_format($value, $show_zero_price = true) {

    $price = price_parse($value);

    $price = number_format($price, 2, '.', ' ');

    if (!$show_zero_price) {

        $price_test = price_parse($price);

        if (empty($price_test)) {
            return '';
        }
    }

    return $price;
}

function price_parse($value) {

    return round((float) str_replace(array(',', ' '), array('.', ''), trim((string) $value)), 2);
}

Testing:
Code:
echo price_format('5').' USD';
echo '<br />';
echo price_format(5).' USD';
echo '<br />';
echo price_format('0');
echo '<br />';
echo price_format('0.0');
echo '<br />';
echo price_format(0);
echo '<br />';
echo price_format(0.0);
echo '<br />';
echo price_format('0', false);
echo '<br />';
echo price_format('0.0', false);
echo '<br />';
echo price_format(0, false);
echo '<br />';
echo price_format(0.0, false);
echo '<br />';
echo price_format('5 000').' USD';
echo '<br />';
echo price_format('5 000.0').' USD';
echo '<br />';
echo price_format(5000).' USD';
echo '<br />';
echo price_format(5000000).' USD';
echo '<br />';
echo price_format(-5000000).' USD';
Reply


Messages In This Thread
Shopping Cart and leading zeros issue - by castle - 03-10-2017, 12:08 AM
RE: Shopping Cart and leading zeros issue - by ivantcholakov - 03-10-2017, 07:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB