Subtraction function

Hi there.

I am currently using a php based shopping cart program.

Unfortunately when adding 20% vat, it rounds up the gross price to whole number. For example if the net price is £10.83 it rounds up to £13.00 when I need it to be £12.99.

The line of code that outputs the figure is below.

[php]<?php echo money($calcrate * $product["price_low"],'local'); ?>[/php]

What I am trying to do is wrap the money function within a separate function that will detect a certain number (13.00) and then deduct 0.01 to give me the correct figure.

Can anyone help me with this?

Many thanks.

Do you have code for the money() function.

£10.83 + 20% VAT is 12.996 (which is why its rounding up to £13.00) - so we somehow need to ignore the .6 pence for you, which should be possible inside money();

Thanks,

Hi.

Thanks for helping me Williamcameron.

I think the code you need is here:

[php]function money($theNum) {
$cwLocaleInfo = localeconv();
$retStr = number_format($theNum,
2,
$cwLocaleInfo[“mon_decimal_point”],
$cwLocaleInfo[“mon_thousands_sep”]);
if ($cwLocaleInfo[“p_cs_precedes”]) {
if ($cwLocaleInfo[“p_sep_by_space”]) {
$retStr = " ".$retStr;
}
$retStr = $cwLocaleInfo[“currency_symbol”].$retStr;
} else {
if ($cwLocaleInfo[“p_sep_by_space”]) {
$retStr .= " ";
}
$retStr .= $cwLocaleInfo[“currency_symbol”];
}
return $retStr;
}[/php]

Let me know if you need anything else.

Thanks again.

Hi granvillescloth, no problem.

You can fix this 1 of 2 ways.
Either in the money() function if you have access to this:

[php]function money($theNum) {
$theNum = floor($theNum*100)/100; // Add this line to round down 2dp…
$cwLocaleInfo = localeconv();
$retStr = number_format($theNum, //…
[/php]

alternatively, in your original code, the figure you are passing into money(); can be set before hand
[php]<?php echo money(floor($calcrate * $product["price_low"] * 100)/100, 'local'); // Or try this. ?>[/php]

Hope this is useful.

Hi Williamcameron.

I cannot thank you enough. Worked like a charm.

Had no idea about the floor() function. Will keep that in mind for future use. I am sure it will come in very handy.

Quite surprised that this piece of shopping cart software does not have it included as standard tbh.

Thanks for all of your help mate.

Hi Williamcameron.

Just one slight issue I have found though.

It works perfectly on the products page but when the price is listed in the shopping cart, it still shows as £13.00.

The code that is displaying it in the cart page is:

[php]?php echo money($moduleCartItem[“total”],‘local’);?>[/php]

I have tried adding the floor function to the code directly, but this does not do anything.

Do you have any suggestions ?

Thank you for all of your help so far.

Do the same bit of code, but it might be easier to put it in the money function since it seems to be used in more places.

Hi Richei.

Thanks for your response.

Do you know how I would be able to do this ? I cannot get the syntax correct.

Hi granvillesCloth,

Inside money(); - the first line should adjust the $theNum value before it is processed any more.

[php]
$theNum = floor($theNum*100)/100;
[/php]

[php]
function money($theNum) {
$theNum = floor($theNum*100)/100; // Round any half pennies down
$cwLocaleInfo = localeconv();
$retStr = number_format($theNum,
2,
$cwLocaleInfo[“mon_decimal_point”],
$cwLocaleInfo[“mon_thousands_sep”]);
if ($cwLocaleInfo[“p_cs_precedes”]) {
if ($cwLocaleInfo[“p_sep_by_space”]) {
$retStr = " ".$retStr;
}
$retStr = $cwLocaleInfo[“currency_symbol”].$retStr;
} else {
if ($cwLocaleInfo[“p_sep_by_space”]) {
$retStr .= " ";
}
$retStr .= $cwLocaleInfo[“currency_symbol”];
}
return $retStr;
}
[/php]

Hi Williamcameron.

It works fine on the display page, but does not seem to draw through to the cart page. It still shows as £13.00.

No worries mate. I will keep looking on the cart pages to see if I can find the section that needs to be amended.

Thanks for all of your help.

$moduleCartItem[“total”] must already be £13.00.

Possibly look at the cart building section of the code, in particular where the “total” is being calculated.

I think I have found the section of code that adds the total in the cart:
[php]
// set item tax
$cart[“carttotals”][“tax”] = $cart[“carttotals”][“tax”] + $cartItem[“tax”];
// cartItem Total is the SubTotal plus any applicable taxes
$cartItem[“total”] = $cartItem[“subTotal”] + $cartItem[“tax”];[/php]

However, when I applied your solution to cartItem Total, if gives a total in the cart of £2.27.

I appreciate this may be one of them queries that I will just have to work around by changing the price, but do you have any final thoughts on this one ?

I also found the functions that are calculating the tax if that is any help:

[php]// // ---------- // calculateTax: calculate tax on a given amount with a given percentage // ---------- //
function calculateTax($taxable_total,$tax_rate) {
return decimalRound($taxable_total * ($tax_rate/100));
}
// // ---------- // decimalRound: round to 2 places // ---------- //
function decimalRound($number_value) {
return (($number_value * 100))/100;[/php]

}

Sponsor our Newsletter | Privacy Policy | Terms of Service