Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorStefan Giehl <stefan@matomo.org>2019-05-15 02:47:09 +0300
committerdiosmosis <diosmosis@users.noreply.github.com>2019-05-15 02:47:09 +0300
commit045b3c6d936b39ca25623ca40b8ba6b4143f14ba (patch)
tree88206571b9695be9afc31f20a4eec2d152125587 /core
parent0af1f22f728b2bb2232f84ace5f4b5f0c3c60c57 (diff)
Use NumberFormatter to format metric values (#14017)
* Use NumberFormatter to format metric values * fixing tests * update test files * load translations for tests as otherwise number formatter fails * updates expected UI files * ensure correct number of minimum fraction digits * update tests
Diffstat (limited to 'core')
-rw-r--r--core/Metrics/Formatter.php39
-rw-r--r--core/NumberFormatter.php42
2 files changed, 27 insertions, 54 deletions
diff --git a/core/Metrics/Formatter.php b/core/Metrics/Formatter.php
index e5b6e19426..b6593f0f7c 100644
--- a/core/Metrics/Formatter.php
+++ b/core/Metrics/Formatter.php
@@ -27,9 +27,6 @@ class Formatter
{
const PROCESSED_METRICS_FORMATTED_FLAG = 'processed_metrics_formatted';
- private $decimalPoint = null;
- private $thousandsSeparator = null;
-
/**
* Returns a prettified string representation of a number. The result will have
* thousands separators and a decimal point specific to the current locale, eg,
@@ -41,14 +38,7 @@ class Formatter
*/
public function getPrettyNumber($value, $precision = 0)
{
- if ($this->decimalPoint === null) {
- $locale = localeconv();
-
- $this->decimalPoint = $locale['decimal_point'];
- $this->thousandsSeparator = $locale['thousands_sep'];
- }
-
- return number_format($value, $precision, $this->decimalPoint, $this->thousandsSeparator);
+ return NumberFormatter::getInstance()->formatNumber($value, $precision);
}
/**
@@ -154,30 +144,8 @@ class Formatter
*/
public function getPrettyMoney($value, $idSite)
{
- $space = ' ';
$currencySymbol = Site::getCurrencySymbolFor($idSite);
- $currencyBefore = $currencySymbol . $space;
- $currencyAfter = '';
- // (maybe more currencies prefer this notation?)
- $currencySymbolToAppend = array('€', 'kr', 'zł');
- // manually put the currency symbol after the amount
- if (in_array($currencySymbol, $currencySymbolToAppend)) {
- $currencyAfter = $space . $currencySymbol;
- $currencyBefore = '';
- }
- // if the input is a number (it could be a string or INPUT form),
- // and if this number is not an int, we round to precision 2
- if (is_numeric($value)) {
- if ($value == round($value)) {
- // 0.0 => 0
- $value = round($value);
- } else {
- $precision = GoalManager::REVENUE_PRECISION;
- $value = sprintf("%01." . $precision . "f", $value);
- }
- }
- $prettyMoney = $currencyBefore . $value . $currencyAfter;
- return $prettyMoney;
+ return NumberFormatter::getInstance()->formatCurrency($value, $currencySymbol, GoalManager::REVENUE_PRECISION);
}
/**
@@ -190,8 +158,7 @@ class Formatter
*/
public function getPrettyPercentFromQuotient($value)
{
- $result = ($value * 100) . '%';
- return Common::forceDotAsSeparatorForDecimalPoint($result);
+ return NumberFormatter::getInstance()->formatPercent($value * 100, 4, 0);
}
/**
diff --git a/core/NumberFormatter.php b/core/NumberFormatter.php
index 94bfaf7372..52e2cef4f1 100644
--- a/core/NumberFormatter.php
+++ b/core/NumberFormatter.php
@@ -42,6 +42,22 @@ class NumberFormatter
}
/**
+ * Parses the given pattern and returns patterns for positive and negative numbers
+ *
+ * @param string $pattern
+ * @return array
+ */
+ protected function parsePattern($pattern)
+ {
+ $patterns = explode(';', $pattern);
+ if (!isset($patterns[1])) {
+ // No explicit negative pattern was provided, construct it.
+ $patterns[1] = '-' . $patterns[0];
+ }
+ return $patterns;
+ }
+
+ /**
* Formats a given number or percent value (if $value starts or ends with a %)
*
* @param string|int|float $value
@@ -166,22 +182,6 @@ class NumberFormatter
}
/**
- * Parses the given pattern and returns patterns for positive and negative numbers
- *
- * @param string $pattern
- * @return array
- */
- protected function parsePattern($pattern)
- {
- $patterns = explode(';', $pattern);
- if (!isset($patterns[1])) {
- // No explicit negative pattern was provided, construct it.
- $patterns[1] = '-' . $patterns[0];
- }
- return $patterns;
- }
-
- /**
* Formats the given number with the given pattern
*
* @param string $pattern
@@ -236,10 +236,10 @@ class NumberFormatter
// Reconstruct the major digits.
$majorDigits = implode(',', $groups);
}
- if ($minimumFractionDigits < $maximumFractionDigits) {
+ if ($minimumFractionDigits <= $maximumFractionDigits) {
// Strip any trailing zeroes.
$minorDigits = rtrim($minorDigits, '0');
- if (strlen($minorDigits) < $minimumFractionDigits) {
+ if (strlen($minorDigits) && strlen($minorDigits) < $minimumFractionDigits) {
// Now there are too few digits, re-add trailing zeroes
// until the desired length is reached.
$neededZeroes = $minimumFractionDigits - strlen($minorDigits);
@@ -298,4 +298,10 @@ class NumberFormatter
{
return StaticContainer::get('Piwik\NumberFormatter');
}
+
+ public function clearCache()
+ {
+ $this->patterns = [];
+ $this->symbols = [];
+ }
} \ No newline at end of file