本文整理汇总了PHP中Zend_Locale_Math::round方法的典型用法代码示例。如果您正苦于以下问题:PHP Zend_Locale_Math::round方法的具体用法?PHP Zend_Locale_Math::round怎么用?PHP Zend_Locale_Math::round使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend_Locale_Math
的用法示例。
在下文中一共展示了Zend_Locale_Math::round方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: toNumber
/**
* Returns a locale formatted number depending on the given options.
* The seperation and fraction sign is used from the set locale.
* ##0.# -> 12345.12345 -> 12345.12345
* ##0.00 -> 12345.12345 -> 12345.12
* ##,##0.00 -> 12345.12345 -> 12,345.12
*
* @param string $input Localized number string
* @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
* @return string locale formatted number
* @throws Zend_Locale_Exception
*/
public static function toNumber($value, array $options = array())
{
// load class within method for speed
// require_once 'Zend/Locale/Math.php';
$value = Zend_Locale_Math::normalize($value);
$value = Zend_Locale_Math::floatalize($value);
$options = self::_checkOptions($options) + self::$_options;
$options['locale'] = (string) $options['locale'];
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
$oenc = iconv_get_encoding('internal_encoding');
iconv_set_encoding('internal_encoding', 'UTF-8');
// Get format
$format = $options['number_format'];
if ($format === null) {
$format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
$format = self::_seperateFormat($format, $value, $options['precision']);
if ($options['precision'] !== null) {
$value = Zend_Locale_Math::normalize(Zend_Locale_Math::round($value, $options['precision']));
}
} else {
// seperate negative format pattern when available
$format = self::_seperateFormat($format, $value, $options['precision']);
if (strpos($format, '.')) {
if (is_numeric($options['precision'])) {
$value = Zend_Locale_Math::round($value, $options['precision']);
} else {
if (substr($format, iconv_strpos($format, '.') + 1, 3) == '###') {
$options['precision'] = null;
} else {
$options['precision'] = iconv_strlen(iconv_substr($format, iconv_strpos($format, '.') + 1, iconv_strrpos($format, '0') - iconv_strpos($format, '.')));
$format = iconv_substr($format, 0, iconv_strpos($format, '.') + 1) . '###' . iconv_substr($format, iconv_strrpos($format, '0') + 1);
}
}
} else {
$value = Zend_Locale_Math::round($value, 0);
$options['precision'] = 0;
}
$value = Zend_Locale_Math::normalize($value);
}
if (iconv_strpos($format, '0') === false) {
iconv_set_encoding('internal_encoding', $oenc);
// require_once 'Zend/Locale/Exception.php';
throw new Zend_Locale_Exception('Wrong format... missing 0');
}
// get number parts
$pos = iconv_strpos($value, '.');
if ($pos !== false) {
if ($options['precision'] === null) {
$precstr = iconv_substr($value, $pos + 1);
} else {
$precstr = iconv_substr($value, $pos + 1, $options['precision']);
if (iconv_strlen($precstr) < $options['precision']) {
$precstr = $precstr . str_pad("0", $options['precision'] - iconv_strlen($precstr), "0");
}
}
} else {
if ($options['precision'] > 0) {
$precstr = str_pad("0", $options['precision'], "0");
}
}
if ($options['precision'] === null) {
if (isset($precstr)) {
$options['precision'] = iconv_strlen($precstr);
} else {
$options['precision'] = 0;
}
}
// get fraction and format lengths
if (strpos($value, '.') !== false) {
$number = substr((string) $value, 0, strpos($value, '.'));
} else {
$number = $value;
}
$prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
$prec = Zend_Locale_Math::floatalize($prec);
$prec = Zend_Locale_Math::normalize($prec);
if (iconv_strpos($prec, '-') !== false) {
$prec = iconv_substr($prec, 1);
}
if ($prec == 0 and $options['precision'] > 0) {
$prec = "0.0";
}
if ($options['precision'] + 2 > iconv_strlen($prec)) {
$prec = str_pad((string) $prec, $options['precision'] + 2, "0", STR_PAD_RIGHT);
}
if (iconv_strpos($number, '-') !== false) {
$number = iconv_substr($number, 1);
//.........这里部分代码省略.........
示例2: toNumber
/**
* Returns a locale formatted number depending on the given options.
* The seperation and fraction sign is used from the set locale.
* ##0.# -> 12345.12345 -> 12345.12345
* ##0.00 -> 12345.12345 -> 12345.12
* ##,##0.00 -> 12345.12345 -> 12,345.12
*
* @param string $input Localized number string
* @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
* @return string locale formatted number
*/
public static function toNumber($value, array $options = array())
{
// load class within method for speed
require_once 'Zend/Locale/Math.php';
$value = Zend_Locale_Math::normalize($value);
$options = array_merge(self::$_Options, self::checkOptions($options));
if ($options['locale'] instanceof Zend_Locale) {
$options['locale'] = $options['locale']->toString();
}
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
iconv_set_encoding('internal_encoding', 'UTF-8');
// Get format
$format = $options['number_format'];
if ($format === null) {
$format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
if (iconv_strpos($format, ';') !== false) {
if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
$format = iconv_substr($format, iconv_strpos($format, ';') + 1);
} else {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
}
}
} else {
// seperate negative format pattern when available
if (iconv_strpos($format, ';') !== false) {
if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
$format = iconv_substr($format, iconv_strpos($format, ';') + 1);
} else {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
}
}
if (strpos($format, '.')) {
if (is_numeric($options['precision'])) {
$value = Zend_Locale_Math::round($value, $options['precision']);
} else {
if (substr($format, strpos($format, '.') + 1, 3) == '###') {
$options['precision'] = null;
} else {
$options['precision'] = strlen(substr($format, strpos($format, '.') + 1,
strrpos($format, '0') - strpos($format, '.')));
$format = substr($format, 0, strpos($format, '.') + 1) . '###'
. substr($format, strrpos($format, '0') + 1);
}
}
} else {
$value = Zend_Locale_Math::round($value, 0);
$options['precision'] = 0;
}
$value = Zend_Locale_Math::normalize($value);
}
// get number parts
if (strlen($value) != strlen(Zend_Locale_Math::round($value, 0))) {
if ($options['precision'] === null) {
$precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1);
} else {
$precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1, $options['precision']);
if (iconv_strlen($precstr) < $options['precision']) {
$precstr = $precstr . str_pad("0", ($options['precision'] - iconv_strlen($precstr)), "0");
}
}
} else {
if ($options['precision'] > 0) {
$precstr = str_pad("0", ($options['precision']), "0");
}
}
if ($options['precision'] === null) {
if (isset($precstr)) {
$options['precision'] = iconv_strlen($precstr);
} else {
$options['precision'] = 0;
}
}
// get fraction and format lengths
if (strpos($value, '.') !== false) {
$number = substr((string) $value, 0, strpos($value, '.'));
} else {
$number = $value;
}
$prec = call_user_func(Zend_Locale_Math::$sub, $value, $number, $options['precision']);
if (iconv_strpos($prec, '-') !== false) {
//.........这里部分代码省略.........
示例3: getValue
/**
* Returns the internal value
*
* @param integer $round (Optional) Rounds the value to an given precision,
* Default is 2, -1 returns without rounding
*/
public function getValue($round = 2)
{
if ($round < 0) {
return $this->_value;
}
return Zend_Locale_Math::round($this->_value, $round);
}
示例4: setType
/**
* Set a new type, and convert the value
*
* @param string $type New type to set
* @throws Zend_Measure_Exception
*/
public function setType($type)
{
if (empty($this->_units[$type])) {
require_once 'Zend/Measure/Exception.php';
throw new Zend_Measure_Exception("Type ({$type}) is unknown");
}
if (empty($this->_type)) {
$this->_type = $type;
} else {
// Convert to standard value
$value = $this->_value;
$prec = 0;
if (strpos($this->_value, '.') !== false) {
$prec = strlen(substr($this->_value, strpos($this->_value, '.') + 1));
}
if (is_array($this->_units[$this->getType()][0])) {
foreach ($this->_units[$this->getType()][0] as $key => $found) {
switch ($key) {
case "/":
if ($found != 0) {
$value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
}
break;
case "+":
$value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
break;
case "-":
$value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
break;
default:
$value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
break;
}
}
} else {
$value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
}
// Convert to expected value
if (is_array($this->_units[$type][0])) {
foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
switch ($key) {
case "/":
$value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
break;
case "+":
$value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
break;
case "-":
$value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
break;
default:
if ($found != 0) {
$value = @call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
}
break;
}
}
} else {
$value = @call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
}
$this->_value = Zend_Locale_Math::round($value, $prec);
$this->_type = $type;
}
return $this;
}
示例5: setType
/**
* Set a new type, and convert the value
*
* @param string $type New type to set
* @throws Zend_Measure_Exception
* @return Zend_Measure_Abstract
*/
public function setType($type)
{
if (empty($this->_units[$type])) {
require_once 'Zend/Measure/Exception.php';
throw new Zend_Measure_Exception("Type ({$type}) is unknown");
}
if (empty($this->_type)) {
$this->_type = $type;
} else {
// Convert to standard value
$value = $this->_value;
if (is_array($this->_units[$this->getType()][0])) {
foreach ($this->_units[$this->getType()][0] as $key => $found) {
switch ($key) {
case "/":
if ($found != 0) {
$value = call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
}
break;
case "+":
$value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
break;
case "-":
$value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
break;
default:
$value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
break;
}
}
} else {
$value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
}
// Convert to expected value
if (is_array($this->_units[$type][0])) {
foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
switch ($key) {
case "/":
$value = call_user_func(Zend_Locale_Math::$mul, $value, $found, 25);
break;
case "+":
$value = call_user_func(Zend_Locale_Math::$sub, $value, $found, 25);
break;
case "-":
$value = call_user_func(Zend_Locale_Math::$add, $value, $found, 25);
break;
default:
if ($found != 0) {
$value = call_user_func(Zend_Locale_Math::$div, $value, $found, 25);
}
break;
}
}
} else {
$value = call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
}
$slength = strlen($value);
$length = 0;
for ($i = 1; $i <= $slength; ++$i) {
if ($value[$slength - $i] != '0') {
$length = 26 - $i;
break;
}
}
$this->_value = Zend_Locale_Math::round($value, $length);
$this->_type = $type;
}
return $this;
}
示例6: _round
/**
* Round $number with set $precision
*
* @param float $number Number to round
* @param integer $precision Precision
*/
protected function _round($number, $precision)
{
return Zend_Locale_Math::round($number, $precision);
}
示例7: testNegativeRounding
public function testNegativeRounding()
{
$this->assertEquals('-3', Zend_Locale_Math::round('-3.4'));
$this->assertEquals(round(-3.4), Zend_Locale_Math::round('-3.4'));
$this->assertEquals('-4', Zend_Locale_Math::round('-3.5'));
$this->assertEquals(round(-3.5), Zend_Locale_Math::round('-3.5'));
$this->assertEquals('-4', Zend_Locale_Math::round('-3.6'));
$this->assertEquals(round(-3.6), Zend_Locale_Math::round('-3.6'));
$this->assertEquals('-4', Zend_Locale_Math::round('-3.6', 0));
$this->assertEquals(round(-3.6, 0), Zend_Locale_Math::round('-3.6', 0));
$this->assertEquals('-1.96', Zend_Locale_Math::round('-1.95583', 2), '', 0.02);
$this->assertEquals(round(-1.95583, 2), Zend_Locale_Math::round('-1.95583', 2), '', 0.02);
$this->assertEquals(-1242000, Zend_Locale_Math::round('-1241757', -3), '', 250);
$this->assertEquals(round(-1241757, -3), Zend_Locale_Math::round('-1241757', -3), '', 250);
$this->assertEquals(-5.05, Zend_Locale_Math::round('-5.045', 2), '', 0.02);
$this->assertEquals(round(-5.045, 2), Zend_Locale_Math::round('-5.045', 2), '', 0.02);
$this->assertEquals(-5.06, Zend_Locale_Math::round('-5.055', 2), '', 0.02);
$this->assertEquals(round(-5.055, 2), Zend_Locale_Math::round('-5.055', 2), '', 0.02);
}
示例8: set_include_path
set_include_path(get_include_path() . PATH_SEPARATOR . './library');
// setup Zend autloading, this requires that the library is available via the include_path
require 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
// define float value that will differ between rounding and cutoff
$floatvalue = 1298.85525;
$precision = 2;
$testLocale = 'en_GB';
// setup a fixed locale to get around differeing output formats regarding . and ,
$locale = new Zend_Locale();
$locale->setLocale('en_GB');
Zend_Registry::set('Zend_Locale', $locale);
// what is produced by php round function
// expected result is 1298.86
echo round($floatvalue, $precision);
echo "\n";
// what is produce by Zend_Locale_Math
// expected result is 1298.86
echo Zend_Locale_Math::round($floatvalue, $precision);
echo "\n";
// what is produced by Zend_Locale_Math without specifiying an explicit format
// expected result is 1,298.86
// this will return 1,298.85 instead!
echo Zend_Locale_Format::toNumber($floatvalue, array('precision' => $precision, 'locale' => $locale));
echo "\n";
// what is produced by Zend_Locale_Math with specifiying an explicit format
// expected result is 1,298.86
$format = Zend_Locale_Data::getContent((string) $locale, 'decimalnumber');
echo Zend_Locale_Format::toNumber($floatvalue, array('precision' => $precision, 'locale' => $locale, 'number_format' => $format));
echo "\n";
示例9: toNumber
/**
* Returns a locale formatted number depending on the given options.
* The seperation and fraction sign is used from the set locale.
* ##0.# -> 12345.12345 -> 12345.12345
* ##0.00 -> 12345.12345 -> 12345.12
* ##,##0.00 -> 12345.12345 -> 12,345.12
*
* @param string $input Localized number string
* @param array $options Options: number_format, locale, precision. See {@link setOptions()} for details.
* @return string locale formatted number
*/
public static function toNumber($value, array $options = array())
{
print "\ntoNumber";
print "\n--------";
// load class within method for speed
require_once 'Zend/Locale/Math.php';
$value = Zend_Locale_Math::normalize($value);
print "\n1-Value :'" . $value . "'";
$options = array_merge(self::$_Options, self::checkOptions($options));
if ($options['locale'] instanceof Zend_Locale) {
$options['locale'] = $options['locale']->toString();
}
print "\n2-Locale:'" . $options['locale'] . "'";
// Get correct signs for this locale
$symbols = Zend_Locale_Data::getList($options['locale'], 'symbols');
iconv_set_encoding('internal_encoding', 'UTF-8');
// Get format
$format = $options['number_format'];
print "\n3-Format1:'" . $format . "'";
if ($format === null) {
$format = Zend_Locale_Data::getContent($options['locale'], 'decimalnumber');
print "\n3-Format2:'" . $format . "'";
if (iconv_strpos($format, ';') !== false) {
if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
$format = iconv_substr($format, iconv_strpos($format, ';') + 1);
print "\n3-Format3:'" . $format . "'";
} else {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
print "\n3-Format4:'" . $format . "'";
}
}
} else {
print "\n3-Format5:'" . $format . "'";
// seperate negative format pattern when avaiable
if (iconv_strpos($format, ';') !== false) {
if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
$format = iconv_substr($format, iconv_strpos($format, ';') + 1);
print "\n3-Format6:'" . $format . "'";
} else {
$format = iconv_substr($format, 0, iconv_strpos($format, ';'));
print "\n3-Format7:'" . $format . "'";
}
}
if (strpos($format, '.')) {
print "\n4-Prec:'" . $options['precision'] . "'";
if (is_numeric($options['precision'])) {
$value = Zend_Locale_Math::round($value, $options['precision']);
print "\n4-Value1:'" . $value . "'";
//print "\nSet Pr:'".$value."'";
} else {
if (substr($format, strpos($format, '.') + 1, 3) == '###') {
$options['precision'] = null;
print "\n4-Value2";
} else {
$options['precision'] = strlen(substr($format, strpos($format, '.') + 1, strrpos($format, '0') - strpos($format, '.')));
print "\n4-Prec3:'" . $options['precision'] . "'";
$format = substr($format, 0, strpos($format, '.') + 1) . '###' . substr($format, strrpos($format, '0') + 1);
print "\n4-Format4:'" . $format . "'";
}
}
} else {
$value = Zend_Locale_Math::round($value, 0);
$options['precision'] = 0;
print "\n4-Value5:'" . $value . "'";
}
$value = Zend_Locale_Math::normalize($value);
print "\n5-Value:'" . $value . "'";
}
// get number parts
if (strlen($value) != strlen(Zend_Locale_Math::round($value, 0))) {
if ($options['precision'] === null) {
$precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1);
print "\n6-Prec1:'" . $precstr . "'";
} else {
$precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1, $options['precision']);
print "\n6-Prec2:'" . $precstr . "'";
if (iconv_strlen($precstr) < $options['precision']) {
$precstr = $precstr . str_pad("0", $options['precision'] - iconv_strlen($precstr), "0");
print "\n6-Prec3:'" . $precstr . "'";
}
}
} else {
print "\n6-Prec4";
if ($options['precision'] > 0) {
$precstr = str_pad("0", $options['precision'], "0");
print "\n6-Prec5:'" . $precstr . "'";
}
}
if ($options['precision'] === null) {
//.........这里部分代码省略.........
示例10: setOrderSubtotals
/**
* Calculate and set order MercadoPago specific subtotals based on data values
*
* @param $data
* @param $order
*/
public function setOrderSubtotals($data, $order)
{
if (isset($data['total_paid_amount'])) {
$balance = $this->_getMultiCardValue($data, 'total_paid_amount');
} else {
$balance = $data['transaction_details']['total_paid_amount'];
}
if (isset($data['shipping_cost'])) {
$shippingCost = $this->_getMultiCardValue($data, 'shipping_cost');
$order->setBaseShippingAmount($shippingCost);
$order->setShippingAmount($shippingCost);
} else {
$shippingCost = 0;
}
$order->setGrandTotal($balance);
$order->setBaseGrandTotal($balance);
if ($shippingCost > 0) {
$order->setBaseShippingAmount($shippingCost);
$order->setShippingAmount($shippingCost);
}
$couponAmount = $this->_getMultiCardValue($data, 'coupon_amount');
$transactionAmount = $this->_getMultiCardValue($data, 'transaction_amount');
if ($couponAmount) {
$order->setDiscountCouponAmount($couponAmount * -1);
$order->setBaseDiscountCouponAmount($couponAmount * -1);
$balance = $balance - ($transactionAmount - $couponAmount + $shippingCost);
} else {
$balance = $balance - $transactionAmount - $shippingCost;
}
if (\Zend_Locale_Math::round($balance, 4) > 0) {
$order->setFinanceCostAmount($balance);
$order->setBaseFinanceCostAmount($balance);
}
$order->save();
}
示例11: getAmount
public function getAmount()
{
return Zend_Locale_Math::round($this->amount, 2);
}
示例12: round
/**
* Unified round() implementation for the Datatrans extension
*
* @param mixed $value String, Integer or Float
* @return float
*/
public function round($value)
{
return Zend_Locale_Math::round($value, 2);
}