Skip to content

Instantly share code, notes, and snippets.

@pilif
Created June 4, 2015 15:25
Show Gist options
  • Save pilif/175024a0bcbcb09660a6 to your computer and use it in GitHub Desktop.
Save pilif/175024a0bcbcb09660a6 to your computer and use it in GitHub Desktop.
diff --git a/lib/pstools.php b/lib/pstools.php
index 288d8cc..23ea0b8 100644
--- a/lib/pstools.php
+++ b/lib/pstools.php
@@ -311,18 +311,29 @@ function getHolyDayText($date, $lang=''){
return "";
}
-function prettyfloat($nr, $precision=0, $max=4){
-
+function needed_precision($nr, $precision, $max){
$exp = 0;
while($exp < $max && (string)($nr * pow(10, $exp)) != (string)(round($nr * pow(10, $exp)))){
$exp++;
}
- $precision = max($precision, $exp);
+ return max($precision, $exp);
+}
+
+function prettyfloat($nr, $precision=0, $max=4){
+
+ $precision = needed_precision($nr, $precision, $max);
return (string)L(floatval($nr), $precision, $max);
}
+function split_fraction($number, $precision = 0, $max = 4){
+ $int_part = intval($number);
+ $needed_prec = needed_precision($number, 0, 4);
+ $fraction = intval(round($number - $int_part, $needed_prec) * pow(10, $needed_prec));
+ return [$int_part, $fraction];
+}
+
function &getOrderSetsFromImportFile($filename){
$cpso = Customization::callHook("HandlePsoImport", $filename);
if(isset($cpso)){
diff --git a/lib/smarty/helpers/cebus.php b/lib/smarty/helpers/cebus.php
index 4f90a10..6074202 100644
--- a/lib/smarty/helpers/cebus.php
+++ b/lib/smarty/helpers/cebus.php
@@ -79,7 +79,7 @@ function cebus_find_node_with_prods($cat_node){
function cebus_format_price($price, $unit=null, $class='format_price') {
if($price) {
- list($number, $decimal) = split('[,.]', $price);
+ list($number, $decimal) = split_fraction($price);
$decimal = sprintf('%0-2s', $decimal);
if($unit) $unit = sprintf_html('<small class="unit">%s</small>', $unit);
return sprintf_html('<div class="%s"><span>%s</span><sup class="dec">%s</sup>%s</div>', $class, $number, $decimal, $unit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment