Home > PHP, Smarty > php convert việt nam đồng

php convert việt nam đồng

Ví dụ 1:


function product_price($priceFloat) {
$symbol = 'đ';
$symbol_thousand = '.';
$decimal_place = 0;
$price = number_format($priceFloat, $decimal_place, '', $symbol_thousand);
return $price.$symbol;
}

Cách gọi hàm


$priceFloat = 150000.00;
product_price($priceFloat) //output 150.000đ

Ví dụ 2: Việt Smarty plugin currency format vnd

-1 Tạo một file modifier.vnd.php đặt tại folder …/Smarty/libs/plugins/modifier.vnd.php
-2 Nội dung như sau:


/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.vnd.php
* Type: modifier
* Name: vnd
* Version: 1.0
* Date: April 05th, 2012
* Purpose: Format money to VNĐ
* Install: Drop into the plugin directory.
* Author: nhantam
* Translation to PHP & Smarty: nhantam
*
* Exp:
* {$number|vnd:đ}
* {$number|vnd:vnđ}
-------------------------------------------------------------
*/
function smarty_modifier_vnd($number, $symbol='đ') {
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1.$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
if(!empty($symbol)) $number .= $symbol;
return $number;
}

-3 Sử dụng:


{$number|vnd}
{$number|vnd:đ}
{$number|vnd:vnđ}

nhantam
PHP Developer

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: