How to convert decimal number to words (money format) using PHP?

后端 未结 12 857
野趣味
野趣味 2020-12-19 03:38

I just need a little help here. Because I am creating a code for converting decimals to Money format in words. For example if

I have this number

<

相关标签:
12条回答
  • For Indian Currnecy 
    For Indian Currency
    <html>
    <head>
    <title> Number to word converter</title>
    </head>
    <body>
    <?php
    $ones =array('',' One',' Two',' Three',' Four',' Five',' Six',' Seven',' Eight',' Nine',' Ten',' Eleven',' Twelve',' Thirteen',' Fourteen',' Fifteen',' Sixteen',' Seventeen',' Eighteen',' Nineteen');
    $tens = array('','',' Twenty',' Thirty',' Fourty',' Fifty',' Sixty',' Seventy',' Eighty',' Ninety',);
    $triplets = array('',' Thousand',' Lac',' Crore',' Arab',' Kharab');
    
    
    function Show_Amount_In_Words($num) {
      global $ones, $tens, $triplets;
    $str ="";
    
    
    //$num =(int)$num;
    $th= (int)($num/1000); 
    $x = (int)($num/100) %10;
    $fo= explode('.',$num);
    
    if($fo[0] !=null){
    $y=(int) substr($fo[0],-2);
    
    }else{
        $y=0;
    }
    
    if($x > 0){
        $str =$ones[$x].' Hundred';
    
    }
    if($y>0){
    if($y<20)
    {
     $str .=$ones[$y];
    
    }
    else {
        $str .=$tens[($y/10)].$ones[($y%10)];
       }
    }
    $tri=1;
    while($th!=0){
    
        $lk = $th%100;
        $th = (int)($th/100);
        $count =$tri;
    
        if($lk<20){
            if($lk == 0){
            $tri =0;}
            $str = $ones[$lk].$triplets[$tri].$str;
            $tri=$count;
            $tri++;
        }else{
            $str = $tens[$lk/10].$ones[$lk%10].$triplets[$tri].$str;
            $tri++;
        }
    }
    $num =(float)$num;
    if(is_float($num)){
         $fo= (String) $num;
          $fo= explode('.',$fo);
           $fo1= @$fo[1];
    
    }else{
        $fo1 =0;
    }
    $check = (int) $num;
     if($check !=0){
              return $str.' Rupees'.forDecimal($fo1);
        }else{
           return forDecimal($fo1);
        }
    }//End function Show_Amount_In_Words
    
    if(isset($_POST['num'])){
       $num = $_POST['num'];
     echo Show_Amount_In_Words($num);
     }
    
    
    
    //function for decimal parts
     function forDecimal($num){
        global $ones,$tens;
        $str="";
        $len = strlen($num);
        if($len==1){
            $num=$num*10;
        }
        $x= $num%100;
        if($x>0){
        if($x<20){
            $str = $ones[$x].' Paise';
        }else{
            $str = $tens[$x/10].$ones[$x%10].' Paise';
        }
        }
         return $str;
     }  
    ?>
    <form action='#' method='post'>
    <input type='text' name='num' size='20'>
    <input type='submit' name='submit' />
    </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-19 03:52

    There's a PEAR library that can do this.

    EDIT

    Or you can, at the end of your code, do this

    echo $test . ' cents';
    
    0 讨论(0)
  • 2020-12-19 03:56

    I don't know about cents but I have created code for Indian Rupees.

    <?php
    
    echo getIndianCurrency(2851.05);
    
    
    function getIndianCurrency(float $number)
    {
        $decimal = round($number - ($no = floor($number)), 2) * 100;
        $decimal_part = $decimal;
        $hundred = null;
        $hundreds = null;
        $digits_length = strlen($no);
        $decimal_length = strlen($decimal);
        $i = 0;
        $str = array();
        $str2 = array();
        $words = array(0 => '', 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 => 'forty', 50 => 'fifty', 60 => 'sixty',
            70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
        $digits = array('', 'hundred','thousand','lakh', 'crore');
    
        while( $i < $digits_length ) {
            $divider = ($i == 2) ? 10 : 100;
            $number = floor($no % $divider);
            $no = floor($no / $divider);
            $i += $divider == 10 ? 1 : 2;
            if ($number) {
                $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
                $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
                $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
            } else $str[] = null;
        }
    
        $d = 0;
        while( $d < $decimal_length ) {
            $divider = ($d == 2) ? 10 : 100;
            $decimal_number = floor($decimal % $divider);
            $decimal = floor($decimal / $divider);
            $d += $divider == 10 ? 1 : 2;
            if ($decimal_number) {
                $plurals = (($counter = count($str2)) && $decimal_number > 9) ? 's' : null;
                $hundreds = ($counter == 1 && $str2[0]) ? ' and ' : null;
                @$str2 [] = ($decimal_number < 21) ? $words[$decimal_number].' '. $digits[$decimal_number]. $plural.' '.$hundred:$words[floor($decimal_number / 10) * 10].' '.$words[$decimal_number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
            } else $str2[] = null;
        }
    
        $Rupees = implode('', array_reverse($str));
        $paise = implode('', array_reverse($str2));
        $paise = ($decimal_part > 0) ? $paise . ' Paise' : '';
        return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
    }
    ?>
    

    Output:

    two thousand eight hundred and fifty one Rupees five Paise

    I will write a post about its code description on guptatreepoint.com

    0 讨论(0)
  • 2020-12-19 03:58
    // three hundred and thirteen rupees and seventy-six paise
    echo getIndianCurrency(313.76);
    
    function getIndianCurrency(float $number)
    {
        $decimal = round($number - ($no = floor($number)), 2) * 100;
        $hundred = null;
        $digits_length = strlen($no);
        $i = 0;
        $str = array();
        $words = array(0 => '', 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 => 'forty', 50 => 'fifty', 60 => 'sixty',
            70 => 'seventy', 80 => 'eighty', 90 => 'ninety');
        $digits = array('', 'hundred','thousand','lakh', 'crore');
    
        while( $i < $digits_length ) {
            $divider = ($i == 2) ? 10 : 100;
            $number = floor($no % $divider);
            $no = floor($no / $divider);
            $i += $divider == 10 ? 1 : 2;
            if ($number) {
                $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
                $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
                $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
            } else $str[] = null;
        }
    
        $rupees = implode('', array_reverse($str));
        $paise = '';
    
        if ($decimal) {
            $paise = 'and ';
            $decimal_length = strlen($decimal);
    
            if ($decimal_length == 2) {
                if ($decimal >= 20) {
                    $dc = $decimal % 10;
                    $td = $decimal - $dc;
                    $ps = ($dc == 0) ? '' : '-' . $words[$dc];
    
                    $paise .= $words[$td] . $ps;
                } else {
                    $paise .= $words[$decimal];
                }
            } else {
                $paise .= $words[$decimal % 10];
            }
    
            $paise .= ' paise';
        }
    
        return ($rupees ? $rupees . 'rupees ' : '') . $paise ;
    }
    

    Uses:

    echo getIndianCurrency(313.76);
    

    Output:

    three hundred and thirteen rupees and seventy-six paise
    
    0 讨论(0)
  • 2020-12-19 04:00
    <?php
    
    $price = mysqli_real_escape_string($conn, $_GET['price']);
    $adprice = mysqli_real_escape_string($conn, $_GET['adprice']);
    $number = $price-$adprice;
    
    $no = round($number);
    $point = round($number - $no, 2) * 100;
    $hundred = null;
    $digits_1 = strlen($no);
    $i = 0;
    $str = array();
    $words = array('0' => '', '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' => 'Forty', '50' => 'Fifty',
        '60' => 'Sixty', '70' => 'Seventy',
        '80' => 'Eighty', '90' => 'Ninety');
    $digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
    while ($i < $digits_1) {
    $divider = ($i == 2) ? 10 : 100;
    $number = floor($no % $divider);
    $no = floor($no / $divider);
    $i += ($divider == 10) ? 1 : 2;
    if ($number) {
        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
        $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
        $str [] = ($number < 21) ? $words[$number] .
                    " " . $digits[$counter] . $plural . " " . $hundred
                    :
                    $words[floor($number / 10) * 10]
                    . " " . $words[$number % 10] . " "
                    . $digits[$counter] . $plural . " " . $hundred;
                } else $str[] = null;
                  }
    $str = array_reverse($str);
    $result = implode('', $str);
    $points = ($point) ?
              "." . $words[$point / 10] . " " . 
              $words[$point = $point % 10] : '';
              echo $result . "  " . $points . "Only ";
     ?> 
    
    0 讨论(0)
  • 2020-12-19 04:00

    Hi i have solved this problem using php

    See this following link Stackoverflow Best Conversion Result for Indian Currency format

    0 讨论(0)
提交回复
热议问题