0

我想将日本货币格式化为标准的“快速可读”格式。

基本上,所有超过 10,000 日元的金额都使用字符“万”(发音为“男人”)书写。

更多关于“万” http://www.romajidesu.com/kanji/%E4%B8%87

因此,1,000,000 日元就是 100 万或“一百人”。这大约是 1,000 美元。

在日本几乎没有人会说“一百万日元”。货币总是分成 10,000 日元的捆绑包。

因此,当您购买汽车时,挡风玻璃上的贴纸上写着(例如)“140 万”

所以,我想显示这种格式。但是,我相信 number_format 不允许将分隔定义为 10,000,并且 money_format 同样没有帮助。

关于实现这一目标的最佳实践方法有什么想法吗?

总之:

  • 一万日元应该读一万</li>
  • 10万日元应该写成10万</li>
  • 1,000,000 日元应改为 100 万</li>
4

2 回答 2

0

我想出的解决方案与上述类似。

function format_yen_for_display($yenAmount)
{
    /*
        Converts Japanese currency to easily readable local format

        10,000 yen should read 1万
        100,000 yen should read 10万
        1,000,000 yen should read 100万
        1,259,000 yen should read 125万 9,000円
    */

    if($yenAmount > 10000)
    {
        //amount over 1万
        $manYen = floor($yenAmount/10000);

        //amount under 1万
        $remainderYen = ($yenAmount - ($manYen * 10000));

        //concat
        $returnNum = "<span class=\"ylarge\">" . $manYen . "万&lt;/span>";

        //if remainder is more than zero, show it
        if($remainderYen > 0)
        {
            //format remainder with thousands separator
            $remainderYen = number_format($remainderYen);

            $returnNum .= "<span class=\"ysmall\">" . $remainderYen ."円&lt;/span>";
        }
    }
    else
    {
        $returnNum = "<span class=\"ylarge\">" . $yenAmount . "円&lt;/span>";
    }
    return $returnNum;
}
于 2018-10-16T06:32:23.777 回答
-1

我曾经在我的WordPress 插件中写过,

/**
 * convert number expressions to value
 *
 * @assert ("34000") == "3万4000円"
 * @assert ("123456.789") == "12万3457円"
 * @assert ("1234567890") == "12億3456万7890円"
 * @assert ("92610000000000") == "92兆6100億円"
 */
public static function formatInJapanese($value) {
    $isApproximate = false;
    $formatted = '';
    if ($value > 1000000000000) {
        if ($value % 1000000000000 !== 0) {
            $isApproximate = true;
        }
        $unitValue = floor($value / 1000000000000);
        $formatted .= $unitValue . '兆';
        $value -= $unitValue * 1000000000000;
    }
    if ($value > 100000000) {
        if ($value % 100000000 !== 0
            && !$isApproximate) {
            $isApproximate = true;
        }
        $unitValue = floor($value / 100000000);
        $formatted .= $unitValue . '億';
        $value -= $unitValue * 100000000;
    }
    if ($value > 10000) {
        if ($value % 10000 !== 0
            && !$isApproximate) {
            $isApproximate = true;
        }
        $unitValue = floor($value / 10000);
        $formatted .= $unitValue . '万';
        $value -= $unitValue * 10000;
    }
    if ($value != 0) {
        $formatted .= round($value);
    }
    return $formatted . '円';
}

它只适用于 man、oku 和 cho。

快速搜索让我发现 Ruby gem number_to_yen做了类似的事情。

于 2018-10-15T06:53:49.557 回答