Page 1 of 1

How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 1:01 am
by guisauer
If the product price is $25.00, I want Opencart to AUTOMATICALLY display it without the decimals ($25)
If the product price is not round, $25.30, I want Opencart to display it with two decimals ($25.30)

I'm using 1.4.9.1, and I have Opencart set to 2 decimals in the Admin section.

Does anyone have an idea on how to do that?
Thank you so much!

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 1:56 am
by uksitebuilder
Where ever the price is displayed in your templates you would have to wrap the price variable in a php function

example

<?php echo $price; ?>

would become

<?php echo rtrim($price, '.00'); ?>

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 2:38 am
by guisauer
How would you apply it to this?
<?php echo $products[$j]['price']; ?>

Would it be:
<?php echo rtrim($products[$j]['price'], '.00'); ?>

Also, isn't there an easier way to do this, so that it's automated, other than having to replace every single string of code that shows $price manually?

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 4:37 am
by uksitebuilder
You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:15 am
by guisauer
Ok, I'll give that a try! Thanks!

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:15 am
by guisauer
But just in case, is this correct? <?php echo rtrim($products[$j]['price'], '.00'); ?>

How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:22 am
by uksitebuilder
Yes

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:28 am
by Qphoria
uksitebuilder wrote:You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc
I'll do you one better and have you do it at the currency library level as then you only have to do it in once place and it will affect all prices dynamically

EDIT: system/library/currency.php

Change:

Code: Select all

return $string;
to

Code: Select all

return rtrim($string, '.00');

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:37 am
by guisauer
awesome, I'll try it out. Thank you both very much!

How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 5:37 am
by uksitebuilder
Bows to the maestro

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 2:43 pm
by guisauer
Actually, instead of rtrim, I had to use str_replace:

return str_replace('.00','',$string);

apparently rtrim searches character by character, instead of a full string. So if a price was $170.00, rtrim would replace ALL zeros, including the one in the middle, resulting in this: $17

Thank you again!

Re: How to automatically remove decimals when price is round

Posted: Thu Sep 29, 2011 3:31 pm
by uksitebuilder
Makes a mental not to be careful with rtrim() and only use for single character trimming

Re: How to automatically remove decimals when price is round

Posted: Tue Nov 15, 2011 6:48 am
by PetracheNicolae
Qphoria wrote:
uksitebuilder wrote:You could probably do it at the controller level because that is where the currency/tax gets calculated.

catalog/model/catalog/product.php
catalog/model/catalog/category.php

etc
I'll do you one better and have you do it at the currency library level as then you only have to do it in once place and it will affect all prices dynamically

EDIT: system/library/currency.php

Change:

Code: Select all

return $string;
to
return rtrim($string, '.00');
can you tell me please how can i make oc calculate prices with just 2 decimals instead of 4? thanks