Skip to content

Instantly share code, notes, and snippets.

@luizs81
Last active December 30, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizs81/7845692 to your computer and use it in GitHub Desktop.
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.
<?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' ? '&yen&nbsp' . $value : $value . '&nbsp円';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment