Alright, so… again let me repeat the fact that I’m only a hobbyist when it comes to programming, AND this is my first day playing with IP so please bear with me.
So my issue was that when I tried to enter a price that had more than 2 decimal places, it would round up. I deal with a lot of prices that have 3 decimal places, such as $0.0395.
I resolved this by the following:
- Updated
ip_products:product_price
type from decimal(20,2)
to float
- Edited
/application/helpers/number_helper.php
On number_helper.php
, below line 25
I added the following:
$decimal_places = (strlen(substr(strrchr($amount, "."), 1)) < 2) ? 2 : strlen(substr(strrchr($amount, "."), 1));
If you notice, it counts the decimals places of $amount
and if it’s less than 2, it default to two. If it’s not less than 2, it will return however many decimal places $amount
has.
For example, if $amount
is 1.10
or 1.1
it will be changed to 1.1
but the line above will still use 2 decimal places. If $amount
is something like 12.98765
, it will use 5 decimal places.
Then on the if
statement at line 28-34
, I change it to this:
if ($currency_symbol_placement == 'before') {
return $currency_symbol . number_format($amount, ($decimal_point) ? $decimal_places : 0, $decimal_point, $thousands_separator);
} elseif ($currency_symbol_placement == 'afterspace') {
return number_format($amount, ($decimal_point) ? $decimal_places : 0, $decimal_point, $thousands_separator) . ' ' . $currency_symbol;
} else {
return number_format($amount, ($decimal_point) ? $decimal_places : 0, $decimal_point, $thousands_separator) . $currency_symbol;
}
I also had to add edit the format_amount
function at line 43 with the following:
if ($amount) {
$CI =& get_instance();
$thousands_separator = $CI->mdl_settings->setting('thousands_separator');
$decimal_point = $CI->mdl_settings->setting('decimal_point');
$decimal_places = (strlen(substr(strrchr($amount, "."), 1)) < 2) ? 2 : strlen(substr(strrchr($amount, "."), 1));
return number_format($amount, ($decimal_point) ? $decimal_places : 0, $decimal_point, $thousands_separator);
}
return null;
That’s what I did for a temporary fix. I’ll create a better way and submit a PR if you guys if you’re okay with this. Now, in the products prices I can use as many decimal places as I’d like: