/**
  * Creates a number formatter according to options and with predefined formats.
  *
  * @param array $options
  *
  * @return \NumberFormatter
  */
 public function create(array $options)
 {
     $options = $this->resolve($options);
     $formatter = new \NumberFormatter($options['locale'], $options['type']);
     if (null !== $options['number_format']) {
         $formatter->setPattern($options['number_format']);
     }
     return $formatter;
 }
 public function convert()
 {
     $formatter = new \NumberFormatter($this->locale, \NumberFormatter::PATTERN_DECIMAL);
     foreach ($this->binaryPrefixes as $size => $unitPattern) {
         if ($size <= $this->number) {
             $value = $this->number >= self::CONVERT_THRESHOLD ? $this->number / (double) $size : $this->number;
             $formatter->setPattern($unitPattern);
             return $formatter->format($value);
         }
     }
     return $formatter->format($this->number);
 }
 public static function moneda($entero, $localidad = null, $codigo = null)
 {
     if (empty($entero)) {
         return $entero;
     }
     $localidad = empty($localidad) ? self::LOCALIDAD_RD : $localidad;
     $codigo = empty($codigo) ? self::CODIGO_RD : $codigo;
     $sufijo = $localidad == 'es_DO' ? 'RD' : '';
     $fmt = new NumberFormatter($localidad, NumberFormatter::CURRENCY);
     //$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'DOP');
     $fmt->setPattern(str_replace('¤#', '¤ #', $fmt->getPattern()));
     return $sufijo . $fmt->formatCurrency($entero, $codigo);
     //setlocale(LC_MONETARY, $localidad);
     //return money_format('%i', $entero) . "\n";
 }
Beispiel #4
0
 public function formatUang($value, $currency)
 {
     /* if($currency == 'Rupee India'){
     			$fmt = new NumberFormatter( 'en_IN', NumberFormatter::CURRENCY );		
     		}else{
     			$fmt = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );		
     		}
     		
     		if($currency == 'Rupiah Indonesia'){
     			$currency = 'IDR';
     		}elseif($currency == 'Dollar Amerika'){
     			$currency = 'USD';
     		}elseif($currency == 'Dollar Singapura'){
     			$currency = 'SGD';
     		}elseif($currency == 'Baht Thailand'){
     			$currency = 'THB';
     		}elseif($currency == 'Peso Argentina'){
     			$currency = 'ARS';
     		}elseif($currency == 'Ringgit Malaysia'){
     			$currency = 'MYR';
     		}elseif($currency == 'Yen Jepang'){
     			$currency = 'JPY';
     		} */
     if ($currency == 'INR') {
         $fmt = new NumberFormatter('en_IN', NumberFormatter::CURRENCY);
     } else {
         $fmt = new NumberFormatter('IND', NumberFormatter::CURRENCY);
     }
     $sMyPattern = "¤ #,##0.00;-¤ #,##0.00";
     $fmt->setPattern($sMyPattern);
     return $fmt->formatCurrency($value, $currency);
 }
Beispiel #5
0
 /**
  * Formato numero conforme locale
  *
  * @param int   $valor    Integer Valor
  * @param float $decimais Float Decimais
  *
  * @return string
  * @example : formatNumber(8712.335) = 8.712,34
  */
 function formatNumber($valor, $decimais = 2)
 {
     $valor = floatval($valor);
     $decimais = intval($decimais);
     $pattern = sprintf('#,##0.%s', str_pad('', $decimais, '0'));
     $fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::DECIMAL);
     $fmt->setPattern($pattern);
     return $fmt->format($valor);
 }
Beispiel #6
0
 protected function prepareDemo11Grid()
 {
     $input = new InputSource($_GET);
     $grid = new Grid($provider = $this->getDataProvider(), [new TableCaption('Demo 11: Customization. Table Caption'), new Column('id'), new Column('name'), new Column('role'), new Column('birthday'), (new Column('age'))->setValueCalculator(function ($row) {
         return DateTime::createFromFormat('Y-m-d', $row->birthday)->diff(new DateTime('now'))->y;
     })->setValueFormatter(function ($val) {
         return "{$val} years";
     }), (new Column('income'))->setValueFormatter(function ($value) {
         if (!class_exists('\\NumberFormatter')) {
             return '$' . $value;
         }
         static $numberFormatter;
         if ($numberFormatter === null) {
             $numberFormatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
             $numberFormatter->setPattern('<span style="color:green">¤</span>#,##0.00;-<span style="color:red">¤</span>#,##0.00');
         }
         return $numberFormatter->format($value);
     }), new FilterControl('name', FilterOperation::OPERATOR_EQ, $input('name')), (new FilterControl('role', FilterOperation::OPERATOR_EQ, $input('role')))->setView(new TemplateView('select', ['options' => ['' => 'All Roles', 'User' => 'Users', 'Manager' => 'Managers', 'Admin' => 'Admins']])), new PageSizeSelectControl($input('ps', 10), [5, 10, 20, 50, 100]), new CsvExport($input('csv')), new ResetButton(), new PageTotalsRow(['id' => function () {
         return 'Page totals';
     }, 'age' => PageTotalsRow::OPERATION_AVG, 'income' => PageTotalsRow::OPERATION_SUM]), new PaginationControl($input('page', 1), 10, $provider), new ColumnSortingControl('id', new InputOption('sort', $_GET)), new ColumnSortingControl('birthday', new InputOption('sort', $_GET))]);
     $grid->getTileRow()->detach()->attachTo($grid->getTableHeading());
     $grid->attachTo($this->layout());
 }
Beispiel #7
0
 /**
  * Mostra o Valor no real Formatado
  * @param float $number
  * @param boolean $fixed
  * @param boolean $symbol
  * @param integer $decimals
  * @return string
  */
 public static function nReal($number, $decimals = 2, $symbol = true, $fixed = true)
 {
     if (is_null($number) || empty(self::onlyNumbers($number))) {
         return '';
     }
     $formater = new \NumberFormatter("pt-BR", \NumberFormatter::CURRENCY);
     $formater->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $fixed ? $decimals : 1);
     if ($decimals === false) {
         $decimals = 2;
         preg_match_all('/[0-9][^0-9]([0-9]+)/', $number, $matches);
         if (!empty($matches[1])) {
             $decimals = strlen(rtrim($matches[1][0], 0));
         }
     }
     $formater->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
     if (!$symbol) {
         $pattern = preg_replace("/[¤]/", '', $formater->getPattern());
         $formater->setPattern($pattern);
     } else {
         // ESPAÇO DEPOIS DO SIMBOLO
         $pattern = str_replace("¤", "¤ ", $formater->getPattern());
         $formater->setPattern($pattern);
     }
     return $formater->formatCurrency($number, $formater->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
 }