How to convert a number into a human-readable form? We will allow us to build an invoice for payment: 2000.00 -> Two thousand rubles 00 kopecks

How to convert to make pennies? I try to use intl like this:

$f = new NumberFormatter("ru", NumberFormatter::SPELLOUT); echo $f->format(1432.88); 

It gives "one thousand four hundred thirty two comma eight eight." How to fix it?

  • No, you need to get exactly: "Two thousand rubles 00 kopecks" - entermix
  • And what do you do in advance after the comma, convert the whole part and add the fractional part - Mike
  • Well, this is a crutch..NumberFormatter is specially designed for this, but something is impossible to do so .. - entermix
  • @entermix - NumberFormatter , not MoneyFormatter - Igor

1 answer 1

    
 / **
  * Returns amount in words
  * @author runcore
  * @uses morph (...)
  * /
 function num2str ($ num) {
     $ nul = 'zero';
     $ ten = array (
         array ('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'),
         array ('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'),
     );
     $ a20 = array ('ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen');
     $ tens = array (2 => 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety');
     $ hundred = array ('', 'one hundred', 'two hundred', 'three hundred', 'four hundred', 'five hundred', 'six hundred', 'seven hundred', 'eight hundred', 'nine hundred');
     $ unit = array (// Units
         array ('penny', 'penny', 'kopecks', 1),
         array ('ruble', 'ruble', 'rubles', 0),
         array ('thousand', 'thousand', 'thousand', 1),
         array ('million', 'million', 'million', 0),
         array ('billion, billions, billions, 0),
     );
     //
     list ($ rub, $ kop) = explode ('.', sprintf ("% 015.2f", floatval ($ num)));
     $ out = array ();
     if (intval ($ rub)> 0) {
         foreach (str_split ($ rub, 3) as $ uk => $ v) {// by 3 symbols
             if (! intval ($ v)) continue;
             $ uk = sizeof ($ unit) - $ uk-1;  // unit key
             $ gender = $ unit [$ uk] [3];
             list ($ i1, $ i2, $ i3) = array_map ('intval', str_split ($ v, 1));
             // mega-logic
             $ out [] = $ hundred [$ i1];  # 1xx-9xx
             if ($ i2> 1) $ out [] = $ tens [$ i2]. '  '. $ ten [$ gender] [$ i3];  # 20-99
             else $ out [] = $ i2> 0?  $ a20 [$ i3]: $ ten [$ gender] [$ i3];  # 10-19 |  1-9
             // units without rub & kop
             if ($ uk> 1) $ out [] = morph ($ v, $ unit [$ uk] [0], $ unit [$ uk] [1], $ unit [$ uk] [2]);
         } // foreach
     }
     else $ out [] = $ nul;
     $ out [] = morph (intval ($ rub), $ unit [1] [0], $ unit [1] [1], $ unit [1] [2]);  // rub
     $ out [] = $ kop. '  '.morph ($ kop, $ unit [0] [0], $ unit [0] [1], $ unit [0] [2]);  // kop
     return trim (preg_replace ('/ {2,} /', '', join ('', $ out)));
 }

 / **
  * Decline word form
  * @ author runcore
  * /
 function morph ($ n, $ f1, $ f2, $ f5) {
     $ n = abs (intval ($ n))% 100;
     if ($ n> 10 && $ n1 && $ n

 Взято с https://habrahabr.ru/post/53210/