skip to Main Content

I would like to translate a number into a text for example 300 into three hundred, I am using Laravel 9 inertia js and Vue 3

i need translate a text into a number for example 300 into three hundred

2

Answers


  1. From the php side, you can do this:

    $number = (new NumberFormatter("en", NumberFormatter::SPELLOUT))->format(300);
    // $number = "three hundred"
    
    Login or Signup to reply.
  2. You can do this by using below method, keep your original number and make a copy of your number and converting it into string and pass this from your API to your frontend application so it will do your job flawlessly.

    It will currently give you currency name in indian format you can change it as per your need.

        public function ConvertINWord($number)
        {
            $number = $number;
            $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] : 'Zero';
    
            return $result . "Rupees  " . $points . " Paise";
        }
    
    

    you just need to pass your number to this function and it will return you string of it as you require.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search