承接 johndodev/price-formatter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

johndodev/price-formatter

Composer 安装命令:

composer require johndodev/price-formatter

包简介

Display / Format prices in PHP

README 文档

README

Display prices in PHP.

Requirements

PHP 5.4+

Usage

Basic usage

Defaut settings are designed for Europeans, but you can change it, see Default options chapter.

use Johndodev\PriceFormatter;

// create an instance (see __construct chapter)
$priceFormatter = new PriceFormatter();

echo $priceFormatter->format(4); 
// display "4 €"

echo $priceFormatter->format(4, 'USD'); 
// display 4 $

echo $priceFormatter->format(4, '$'); 
// display 4 $

echo $priceFormatter->format(4, 'USD')->symbolBefore()->symbolSep('');
// display $4

Methods

All methods are chainable:

echo $priceFormatter->format($numberToFormat, $currency = null)

// remove trailing zeros if necessary: 5.00 output 5 but 5.50 output 5.50)
->autoTrailingZeros(true)

// set the symbol separator
->symbolSep(' ')

// set the decimals separator
->decSep('.')

// set the maximum number of decimals to show
->decimals(2)

// put the symbol after the value
->symbolAfter()

// or before
->symbolBefore()

// but you can define the position with a variable        
->symbolPosition(PriceFormatter::SYMBOL_POSITION_AFTER)

// set the thousands separator        
->thousandsSep(',')

// remove trailing zeros (decimals), e.g.: 5.00 will output 5, 5.50 will output 5.5
->trimTrailingZeros(true)

// unbreakable spaces: replace " " by " "
->unbreakable(true);

__construct

You can create as many instances (aka services) as you want with their own defaut options (the next chapter is Default options)

PriceFormatter::__construct($options = [])

$euroFormatter = new PriceFormatter(['currency' => 'EUR']); 
$euroFormatter = new PriceFormatter(['currency' => '']); 

$usdFormatter = new PriceFormatter([
    'currency' => 'USD',
    'symbolPosition' => PriceFormatter::SYMBOL_POSITION_BEFORE,
]);

Default options

You can set all thoses settings per instance, below are default values:

$priceFormatter = new PriceFormatter([
    'currency'          => 'EUR',
    'decimals'          => 2,
    'decSep'            => '.',
    'thousandsSep'      => '',
    'symbolPosition'    => PriceFormatter::SYMBOL_POSITION_AFTER,
    'symbolSep'         => ' ',
    'unbreakable'       => true,
    'trimTrailingZeros' => false,
    'autoTrailingZeros' => true,
]);

Example:

// no spaces between currency and value
$priceFormatter = new PriceFormatter(['symbolSep' => '']);

// display 5€
echo $priceFormatter->format(5); 

// but all options can be overriden for one format()
echo $priceFormatter->format(5)->symbolSep(' ');
// display 5 €

echo $priceFormatter->format(5); 
// then display 5€ again

Currencies and symbols

You can use these code AUD $, CAD $, CHF CHF, CNY ¥, EUR €, GBP £, HKD $, JPY ¥, NOK kr, SEK kr, USD $.

If you want to format an unsupported currency, use the Symbol (http://www.xe.com/symbols.php):

// 5 ฿
echo $priceFormatter->format(5, '฿'); 

Run tests

> composer install
> php vendor/bin/phpunit --testsuite PriceFormatter

统计信息

  • 总下载量: 34
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-03-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固