Skip to content

Instantly share code, notes, and snippets.

@jitendrapurohit
Created November 22, 2016 05:32
Show Gist options
  • Save jitendrapurohit/35827798292e351fd59a2a38bc727352 to your computer and use it in GitHub Desktop.
Save jitendrapurohit/35827798292e351fd59a2a38bc727352 to your computer and use it in GitHub Desktop.
diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php
index f0e76e8..e806a32 100644
--- a/CRM/Price/BAO/PriceField.php
+++ b/CRM/Price/BAO/PriceField.php
@@ -818,10 +818,12 @@ WHERE id IN (" . implode(',', array_keys($priceFields)) . ')';
*/
public static function getTaxLabel($opt, $valueFieldName, $displayOpt, $taxTerm) {
if ($displayOpt == 'Do_not_show') {
- $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
+ $totalAmount = CRM_Utils_Number::roundClosely($opt[$valueFieldName] + $opt['tax_amount']);
+ $label = CRM_Utils_Money::format($totalAmount);
}
elseif ($displayOpt == 'Inclusive') {
- $label = CRM_Utils_Money::format($opt[$valueFieldName] + $opt['tax_amount']);
+ $totalAmount = CRM_Utils_Number::roundClosely($opt[$valueFieldName] + $opt['tax_amount']);
+ $label = CRM_Utils_Money::format($totalAmount);
$label .= '<span class="crm-price-amount-tax"> (includes ' . $taxTerm . ' of ' . CRM_Utils_Money::format($opt['tax_amount']) . ')</span>';
}
else {
diff --git a/CRM/Utils/Number.php b/CRM/Utils/Number.php
index 24c0160..5a8185a 100644
--- a/CRM/Utils/Number.php
+++ b/CRM/Utils/Number.php
@@ -115,4 +115,19 @@ class CRM_Utils_Number {
}
}
+ /**
+ * Round the number to the nearest integer if it
+ * is very close to the next int val.
+ *
+ * @param string $value
+ * @param float $limit
+ *
+ * @return int
+ */
+ public static function roundClosely($value, $limit = 0.05) {
+ $nearestIntVal = round($value);
+ $decimalRound = round($value, 2);
+ return abs($nearestIntVal - $decimalRound) < $limit ? $nearestIntVal : $decimalRound;
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment