Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0, 1.1, 1.2, 1.3] fixed format_currency returns Exponents (closes …

…#5715)

git-svn-id: http://svn.symfony-project.com/branches/1.0@18607 ee427ae8-e902-0410-961c-c3ed070cd9f9
  • Loading branch information...
commit 556691bdd7191dd708352cc0eaa868bb3682f5b0 1 parent 4d16160
fabien authored
View
31 lib/i18n/sfNumberFormat.class.php
@@ -128,11 +128,11 @@ function format($number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8')
// avoid conversion with exponents
// see http://trac.symfony-project.org/ticket/5715
$precision = ini_set('precision', 14);
- $string = (string) $number;
+ $string = $this->fixFloat($number);
ini_set('precision', $precision);
list($number, $decimal) = $this->formatDecimal($string);
- $integer = $this->formatInteger(abs($number));
+ $integer = $this->formatInteger($this->fixFloat(abs($number)));
$result = (strlen($decimal) > 0) ? $integer.$decimal : $integer;
@@ -257,18 +257,21 @@ protected function formatDecimal($string)
}
else if (is_int($decimalDigits))
{
- $string = $float = round((float) $string, $decimalDigits);
- if (strpos((string) $float, '.') === false)
+ if (false === $pos = strpos($string, '.'))
{
$decimal = str_pad($decimal, $decimalDigits, '0');
}
else
{
- $decimal = substr($float, strpos($float,'.') + 1);
- if (strlen($decimal)<$decimalDigits)
+ $decimal = substr($string, $pos + 1);
+ if (strlen($decimal) <= $decimalDigits)
{
$decimal = str_pad($decimal, $decimalDigits, '0');
}
+ else
+ {
+ $decimal = substr($decimal, 0, $decimalDigits);
+ }
}
}
else
@@ -318,4 +321,20 @@ protected function setPattern($pattern)
break;
}
}
+
+ protected function fixFloat($float)
+ {
+ $string = (string) $float;
+
+ if (false === strstr($float, 'E'))
+ {
+ return $string;
+ }
+
+ list($significand, $exp) = explode('E', $string);
+ list(, $decimal) = explode('.', $significand);
+ $exp = str_replace('+', '', $exp) - strlen($decimal);
+
+ return str_replace('.', '', $significand).str_repeat('0', $exp);
+ }
}
View
5 test/unit/helper/NumberHelperTest.php
@@ -12,7 +12,7 @@
sfLoader::loadHelpers(array('Helper', 'Tag', 'Number'));
-$t = new lime_test(4, new lime_output_color());
+$t = new lime_test(7, new lime_output_color());
// format_number()
$t->diag('format_number()');
@@ -23,4 +23,7 @@
// format_currency()
$t->is(format_currency(1200000.00, 'USD', 'en'), 'US$1,200,000.00', 'format_currency() takes a number as its first argument');
+$t->is(format_currency(1200000.1, 'USD', 'en'), 'US$1,200,000.10', 'format_currency() takes a number as its first argument');
+$t->is(format_currency(1200000.10, 'USD', 'en'), 'US$1,200,000.10', 'format_currency() takes a number as its first argument');
+$t->is(format_currency(1200000.101, 'USD', 'en'), 'US$1,200,000.10', 'format_currency() takes a number as its first argument');
$t->is(format_currency('1200000', 'USD', 'en'), 'US$1,200,000.00', 'format_currency() takes a number as its first argument');
Please sign in to comment.
Something went wrong with that request. Please try again.