Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据印度计数系统修改以下代码,将数值表示为文字值_Php_Algorithm - Fatal编程技术网

Php 如何根据印度计数系统修改以下代码,将数值表示为文字值

Php 如何根据印度计数系统修改以下代码,将数值表示为文字值,php,algorithm,Php,Algorithm,下面的代码将数字分类为不同的大小写,并使用默认大小写查找baseunit和数字以及余数,然后递归调用下面的函数将数字表示为单词,从而将数值表示为单词值。我想要实现的是将价值表达到印度的计数系统中,而不是使用百万、亿等 我觉得如果在默认情况下修改这一行:$baseUnit=pow(1000,floor(log($number,1000)) 正确的话,我就能解决这个问题了 我们将非常感谢您的帮助。谢谢 function convert_number_to_words($number) {

下面的代码将数字分类为不同的大小写,并使用默认大小写查找baseunit和数字以及余数,然后递归调用下面的函数将数字表示为单词,从而将数值表示为单词值。我想要实现的是将价值表达到印度的计数系统中,而不是使用百万、亿等

我觉得如果在默认情况下修改这一行:
$baseUnit=pow(1000,floor(log($number,1000))
正确的话,我就能解决这个问题了

我们将非常感谢您的帮助。谢谢

function convert_number_to_words($number) {

    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ', ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'zero',
        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',
        30                  => 'thirty',
        40                  => 'fourty',
        50                  => 'fifty',
        60                  => 'sixty',
        70                  => 'seventy',
        80                  => 'eighty',
        90                  => 'ninety',
        100                 => 'hundred',
        1000                => 'thousand',
        //Instead of the following values I would like to have Indian counting system values
        /*
        1000000             => 'million',
        1000000000          => 'billion',
        1000000000000       => 'trillion',
        1000000000000000    => 'quadrillion',
        1000000000000000000 => 'quintillion'
       */
        100000   => 'lakhs',
        10000000 =>'crore',
        1000000000 =>'arab',
        100000000000  =>'kharab' 
    );

    if (!is_numeric($number)) {
        return false;
    }

    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }

    $string = $fraction = null;

    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }

    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }

    return $string;
}
函数将数字转换为单词($number){
$hyphen='-';
$连接='和';
$separator=',';
$negative='negative';
$decimal='点';
$dictionary=数组(
0=>“零”,
1=>“一”,
2=>“两个”,
3=>‘三’,
4=>“四”,
5=>“五”,
6=>“六”,
7=>“七”,
8=>“八”,
9=>“九”,
10=>“十”,
11=>“11”,
12=>“十二”,
13=>“十三”,
14=>‘十四’,
15=>“十五”,
16=>“十六”,
17=>“17”,
18=>‘十八’,
19=>“十九”,
20=>‘二十’,
30=>“三十”,
40=>“四十”,
50=>“50”,
60=>“六十”,
70=>“70”,
80=>“80”,
90=>90分,
100=>100,
1000=>‘千’,
//我希望使用印度计数系统值,而不是以下值
/*
1000000=>‘百万’,
1000000000=>十亿,
1000000000000=>万亿,
10000000000000=>“万亿美元”,
10000000000000000=>“五百万”
*/
100000=>10万,
10000000=>'crore',
100000000=>“阿拉伯”,
10000000000=>“哈拉布”
);
如果(!是数字($number)){
返回false;
}
如果($number>=0&&(int)$number<0)| |(int)$number<0-PHP_int_MAX){
//溢出
触发错误(
“convert_number_to_words”只接受介于-'.PHP_INT_MAX'和'.PHP_INT_MAX'之间的数字,
用户警告
);
返回false;
}
如果($number<0){
返回$negative。将_number_转换为_单词(abs($number));
}
$string=$fraction=null;
if(strpos($number,'.')!==false){
列表($number,$fraction)=分解('.',$number);
}
开关(真){
案例$number<21:
$string=$dictionary[$number];
打破
案例$number<100:
十元=((整数)($number/10))*10;
$units=$number%10;
$string=$dictionary[$tens];
若有的话(单位){
$string.=$hyphen.$dictionary[$units];
}
打破
案例$number<1000:
$数百=$number/100;
$rements=$number%100;
$string=$dictionary[$数百].'.$dictionary[100];
若有($剩余){
$string.=$conjunction.将数字转换为单词($restinutes);
}
打破
违约:
$baseUnit=pow(1000,楼层(对数($number,1000));
$numBaseUnits=(int)($number/$baseUnit);
$rements=$number%$baseUnit;
$string=将数字转换为单词($numBaseUnits)。''.$dictionary[$baseUnit];
若有($剩余){
$string.=$rements<100?$conjunction:$separator;
$string.=将数字转换为单词($restrict);
}
打破
}
如果(null!=$fraction&&是数值($fraction)){
$string.=$decimal;
$words=array();
foreach(str_分割((字符串)$分数)作为$number){
$words[]=$dictionary[$number];
}
$string.=内爆(“”,$words);
}
返回$string;
}

为了公众利益,我想发布完整的解决方案

下面的PHP函数为我们提供了一种将数字格式转换为用印度计数系统表示的文字格式的方法

感谢Patashu和rici帮助我找到解决问题的方法:-

function convert_number_to_words($number) {
    $hyphen      = '-';
    $conjunction = ' and ';
    $separator   = ', ';
    $negative    = 'negative ';
    $decimal     = ' point ';
    $dictionary  = array(
        0                   => 'zero',
        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',
        30                  => 'thirty',
        40                  => 'fourty',
        50                  => 'fifty',
        60                  => 'sixty',
        70                  => 'seventy',
        80                  => 'eighty',
        90                  => 'ninety',
        100                 => 'hundred',
        1000                => 'thousand',
        //Instead of the following values I would like to have Indian counting system values
        /*
        1000000             => 'million',
        1000000000          => 'billion',
        1000000000000       => 'trillion',
        1000000000000000    => 'quadrillion',
        1000000000000000000 => 'quintillion'
       */
        100000   => 'lakhs',
        10000000 =>'crore',
        1000000000 =>'arab',
        100000000000  =>'kharab' 
    );

    if (!is_numeric($number)) {
        return false;
    }

    if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
        // overflow
        trigger_error(
            'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
            E_USER_WARNING
        );
        return false;
    }

    if ($number < 0) {
        return $negative . convert_number_to_words(abs($number));
    }

    $string = $fraction = null;

    if (strpos($number, '.') !== false) {
        list($number, $fraction) = explode('.', $number);
    }

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens   = ((int) ($number / 10)) * 10;
            $units  = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                $string .= $hyphen . $dictionary[$units];
            }
            break;
        case $number < 1000:
            $hundreds  = $number / 100;
            $remainder = $number % 100;
            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
            if ($remainder) {
                $string .= $conjunction . convert_number_to_words($remainder);
            }
            break;
        default:
            $baseUnit = 10 * pow(100, floor(log($number/10, 100)));// Thanks to rici and Patashu
            $numBaseUnits = (int) ($number / $baseUnit);
            $remainder = $number % $baseUnit;
            $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
            if ($remainder) {
                $string .= $remainder < 100 ? $conjunction : $separator;
                $string .= convert_number_to_words($remainder);
            }
            break;
    }

    if (null !== $fraction && is_numeric($fraction)) {
        $string .= $decimal;
        $words = array();
        foreach (str_split((string) $fraction) as $number) {
            $words[] = $dictionary[$number];
        }
        $string .= implode(' ', $words);
    }

    return $string;
}
函数将数字转换为单词($number){
$hyphen='-';
$连接='和';
$separator=',';
$negative='negative';
$decimal='点';
$dictionary=数组(
0=>“零”,
1=>“一”,
2=>“两个”,
3=>‘三’,
4=>“四”,
5=>“五”,
6=>“六”,
7=>“七”,
8=>“八”,
9=>“九”,
10=>“十”,
11=>“11”,
12=>“十二”,
13=>“十三”,
14=>‘十四’,
15=>“十五”,
16=>“十六”,
17=>“17”,
18=>‘十八’,
19=>'n