【问题标题】:customize format number to flexible自定义格式编号以灵活
【发布时间】:2010-12-29 18:35:59
【问题描述】:

我的php代码如下:

<?php
class number_word {
    private $word_array = array(1=>"One",2=>"Two",3=>"Three",4=>"Four",5=>"Five",6=>"Six",7=>"Seven",8=>"Eight",9=>"Nine",10=>"Ten",11=>"Eleven",12=>"Twelve",13=>"Thirteen",14=>"Fourteen",15=>"Fifteen",16=>"Sixteen",17=>"Seventeen",18=>"Eighteen",19=>"Nineteen",20=>"Twenty",21=>"Twenty-One",22=>"Twenty-Two",23=>"Twenty-Three",24=>"Twenty-Four",25=>"Twenty-Five",26=>"Twenty-Six",27=>"Twenty-Seven",28=>"Twenty-Eight",29=>"Twenty-Nine",30=>"Thirty",31=>"Thirty-One",32=>"Thirty-Two",33=>"Thirty-Three",34=>"Thirty-Four",35=>"Thirty-Five",36=>"Thirty-Six",37=>"Thirty-Seven",38=>"Thirty-Eight",39=>"Thirty-Nine",40=>"Forty",41=>"Forty-One",42=>"Forty-Two",43=>"Forty-Three",44=>"Forty-Four",45=>"Forty-Five",46=>"Forty-Six",47=>"Forty-Seven",48=>"Forty-Eight",49=>"Forty-Nine",50=>"Fifty",51=>"Fifty-One",52=>"Fifty-Two",53=>"Fifty-Three",54=>"Fifty-Four",55=>"Fifty-Five",56=>"Fifty-Six",57=>"Fifty-Seven",58=>"Fifty-Eight",59=>"Fifty-Nine",60=>"Sixty",61=>"Sixty-One",62=>"Sixty-Two",63=>"Sixty-Three",64=>"Sixty-Four",65=>"Sixty-Five",66=>"Sixty-Six",67=>"Sixty-Seven",68=>"Sixty-Eight",69=>"Sixty-Nine",70=>"Seventy",71=>"Seventy-One",72=>"Seventy-Two",73=>"Seventy-Three",74=>"Seventy-Four",75=>"Seventy-Five",76=>"Seventy-Six",77=>"Seventy-Seven",78=>"Seventy-Eight",79=>"Seventy-Nine",80=>"Eighty",81=>"Eighty-One",82=>"Eighty-Two",83=>"Eighty-Three",84=>"Eighty-Four",85=>"Eighty-Five",86=>"Eighty-Six",87=>"Eighty-Seven",88=>"Eighty-Eight",89=>"Eighty-Nine",90=>"Ninety",91=>"Ninety-One",92=>"Ninety-Two",93=>"Ninety-Three",94=>"Ninety-Four",95=>"Ninety-Five",96=>"Ninety-Six",97=>"Ninety-Seven",98=>"Ninety-Eight",99=>"Ninety-Nine",100=>"One Hundred",200=>"Two Hundred",300=>"Three Hundred",400=>"Four Hundred",500=>"Five Hundred",600=>"Six Hundred",700=>"Seven Hundred",800=>"Eight Hundred",900=>"Nine Hundred");

    private $thousand = array("", "Thousand, ", "Million, ", "Billion, ", "Trillion, ", "Zillion, ");

    private $val, $currency0, $currency1;   
    private $val_array, $dec_value, $dec_word, $num_value, $num_word;
    var $val_word;

    public function number_word($in_val = 0, $in_currency0 = "", $in_currency1 = "") {

        $this->val = $in_val;
        $this->currency0 = $in_currency0;
        $this->currency1 = $in_currency1;

        $this->val = abs(floatval(str_replace(",","",$this->val)));

        if ($this->val > 0) {

            $this->val = number_format($this->val, '2', ',', ',');

            $this->val_array = explode(",", $this->val);

            $this->dec_value = intval($this->val_array[count($this->val_array) - 1]);

            if ($this->dec_value > 0) {

                $this->dec_word = $this->word_array[$this->dec_value]." ".$this->currency1;
            }

            $t = 0;

            $this->num_word = "";

            for ($i = count($this->val_array) - 2; $i >= 0; $i--) {

                $this->num_value = intval($this->val_array[$i]);

                if ($this->num_value == 0) {
                    $this->num_word = "".$this->num_word;
                } 

                elseif (strlen($this->num_value."") <= 2) {
                    $this->num_word = $this->word_array[$this->num_value]." ".$this->thousand[$t].$this->num_word;
                    // add 'and' if not last element in VAL
                    if ($i == 1) {
                        $this->num_word = " and ".$this->num_word;
                    }               
                } 
                else {
                    $this->num_word = $this->word_array[substr($this->num_value, 0, 1)."00"]. (intval(substr($this->num_value, 1, 2)) > 0 ? " and " : "") .$this->word_array[intval(substr($this->num_value, 1, 2))]." ".$this->thousand[$t].$this->num_word;
                }
                $t++;
            }       
            if (!empty($this->num_word)) {
                $this->num_word .= " ".$this->currency0;
            }
        }
        $this->val_word = $this->num_word." ".$this->dec_word;
    }
}
?>

上面的代码可以正确读取格式编号 7,052,853.62,但我也希望代码能够读取其他类型的格式编号,例如; 7.052.853,62 或 7 052 853,62 以及当我将逗号更改为行中的点时,例如

    $this->val = abs(floatval(str_replace(",","",$this->val)));
    if ($this->val > 0) {
                    $this->val = number_format($this->val, '2', ',', ',');

                    $this->val_array = explode(",", $this->val);

更新: 测试:

echo $nw->number_word('52,853.62', '美元', '美分');

然后代码无法再读取美分格式数字,我应该怎么做才能修复它?任何指点我都会感激和感谢

【问题讨论】:

  • 你对我朋友的问题是“语言环境”。点和逗号作为千位和小数分隔符的互换适用于德语区域设置。我将阅读更多关于此的内容并尝试发布答案。
  • 看看stackoverflow.com/questions/437371/…。并在 stackoverflow 和 google 中搜索 php locale
  • 嗨 kinjal,是的,我已经阅读了该链接,谢谢,但是关于上面的代码,如何通过计算代码的分隔符来读取格式号?

标签: php numbers format


【解决方案1】:

在对 $this->val 的第一个赋值中,用空格替换除数字之外的任何字符。 即

    public function number_word($in_val = 0, $in_currency0 = "", $in_currency1 = "") {    
        //$this->val = preg_replace('/[^0-9]/', '', $in_val);
        $this->val = str_replace(",", ".", preg_replace('/(\d+)\./', '$1', $in_val)); 
.
.
.
.
.

编辑:1)从正则表达式中删除 g 标志 2) 添加 str_replace 并更改正则表达式以处理德语和美国/英国格式

【讨论】:

  • 感谢您的帮助和感谢,您的代码只有 2 行然后空白? OK 使用后我得到了警告:preg_replace() [function.preg-replace]: Unknown modifier 'g'
  • 嗨 Cyber​​mate,现在我得到了结果,当我输入 52,853.62 时,代码显示为 500 万、28.5 万、362 美元,表示无法读取分,也无法读取数字格式。如果我输入 52.853,62 那么读取的是 500 万、285 万、362 美元,它缺少美分和数字格式,如果我将代码从逗号更改为点也有同样的问题,谢谢。
  • 您能否确认是否会考虑 , 或 .在最后两个字符之前作为小数分隔符?
  • 默认情况下,代码以 (.) 作为小数分隔符读取英国/美国格式数字,但随后我要求代码可以读取德国格式数字 (,) 作为小数分隔符,例如echo $nw->number_word('52,853.62', '美元', '美分');默认情况下(英国/美国),还可以读取 echo $nw->number_word('52.853,62', 'Mark', 'Cent');作为(德国)格式编号。无论如何,感谢您到目前为止的帮助。
  • 是的,我检查了它并且工作了,你太棒了,谢谢你的代码,我只需要设置条件,所以如果我想使用英国/美国格式,那么我想用 $this- >val = $in_val;如果我想使用德国格式,则使用上面的代码,否则它也可以使用法语格式将您的代码从点替换为空格(法语格式,例如 52 853.62 或 52 853,62),对吗?非常感谢
猜你喜欢
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多