Last active
December 30, 2015 14:59
-
-
Save luizs81/7845692 to your computer and use it in GitHub Desktop.
CodeIgniter helper to format numbers to Japanese currency, using the kanjis for thousand and ten thousand.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
if ( ! function_exists('format_yen')) | |
{ | |
/** | |
* Returns a number in Japanese currency format | |
* @param float $value The number to be formated | |
* @param string $symbol_position Define if should use the pre or post aligned kanji | |
* @param string $decimal Define what to do with decimal part of number | |
* @return boolean|string Returns the formated number or FALSE in case of error. | |
*/ | |
function format_yen($value = NULL, $symbol_position = 'pre', $decimal = 'ignore'){ | |
if(is_null($value)) return FALSE; | |
if(!is_numeric($value)) return FALSE; | |
$value = $decimal == 'ignore' ? floor($value) : round($value); | |
if($value >= 10000) | |
{ | |
$ten_thousand = substr($value, -4); | |
$rest = substr($value, 0, -4) . '万'; | |
$value = $ten_thousand == '0000' ? $rest : $rest . $ten_thousand; | |
} | |
if($value >= 1000) | |
{ | |
$thousand = substr($value, -3); | |
$rest = substr($value, 0, -3) . '千'; | |
$value = $thousand == '000' ? $rest : $rest . $thousand; | |
} | |
return $symbol_position == 'pre' ? '¥ ' . $value : $value . ' 円'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment