phpnumberformatter

php NumberFormatter format to spelling


hi im using php NumberFormatter in laravel now if i want to echo number i did this code

@php
   $digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
   echo $digit->format(round(10.557,3));
@endphp

the result will be enter image description here but i need it to be

ten and five handred and seventy five

how can i change the format .. thanks ..


Solution

  • you can try to build a function manually

    function numberToText($number) {
      $digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
      $numberParts = explode('.', (string) round($number,3));
      $formatedNumber =  $digit->format($numberParts[0]);
      if (isset($numberParts[1] ) {
        $formatedNumber .= ' and ' . $digit->format($numberParts[1]);
      }
      return $formatedNumber;
    }
    
    @php
      echo numberToText(10.557);
    @endphp