Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [gotowa funkcja] Liczba słownie
BzikOS
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 660
Pomógł: 0
Dołączył: 28.08.2002
Skąd: Starachowice

Ostrzeżenie: (0%)
-----


Zainspirowany jednym z tematów na forum spróbowałem napisać własną funkcję do zamiany liczby na jej słowną reprezentację. Oto do czego doszedłem:

  1. <?php
  2. function d2w( $digits )
  3. {
  4. $jednosci = Array( 'zero', 'jeden', 'dwa', 'trzy', 'cztery', 'pięć', 'sześć', 'siedem', 'osiem', 'dziewięć' );
  5. $dziesiatki = Array( '', 'dziesięć', 'dwadzieścia', 'trzydzieści', 'czterdzieści', 'piećdziesiąt', 'sześćdziesiąt', 'siedemdziesiąt', 'osiemdziesiąt', 'dziewiećdziesiąt' );
  6. $setki = Array( '', 'sto', 'dwieście', 'trzysta', 'czterysta', 'piećset', 'sześćset', 'siedemset', 'osiemset', 'dziewiećset' );
  7. $nastki = Array( 'dziesieć', 'jedenaście', 'dwanaście', 'trzynaście', 'czternaście', 'piętnaście', 'szesnaście', 'siedemnaście', 'osiemnaście', 'dzięwietnaście' );
  8. $tysiace = Array( 'tysiąc', 'tysiące', 'tysięcy' );
  9.  
  10. $digits = (string) $digits;
  11. $digits = strrev( $digits );
  12. $i = strlen( $digits );
  13.  
  14. $string = '';
  15.  
  16. if( $i > 5 && $digits[5] > 0 )
  17. $string .= $setki[ $digits[5] ] . ' ';
  18. if( $i > 4 && $digits[4] > 1 )
  19. $string .= $dziesiatki[ $digits[4] ] . ' ';
  20. elseif( $i > 3 && $digits[4] == 1 )
  21. $string .= $nastki[$digits[3]] . ' ';
  22. if( $i > 3 && $digits[3] > 0 && $digits[4] != 1 )
  23. $string .= $jednosci[ $digits[3] ] . ' ';
  24.  
  25. $tmpStr = substr( strrev( $digits ), 0, -);
  26. if( strlen( $tmpStr ) > 0 )
  27. {
  28. $tmpInt = (int) $tmpStr;
  29. if( $tmpInt == 1 )
  30. $string .= $tysiace[0] . ' ';
  31. elseif( ( $tmpInt % 10 > 1 && $tmpInt % 10 < 5 ) && ( $tmpInt < 10 || $tmpInt > 20 ) )
  32. $string .= $tysiace[1] . ' ';
  33. else
  34. $string .= $tysiace[2] . ' ';
  35. }
  36.  
  37. if( $i > 2 && $digits[2] > 0 )
  38. $string .= $setki[$digits[2]] . ' ';
  39. if( $i > 1 && $digits[1] > 1 )
  40. $string .= $dziesiatki[$digits[1]] . ' ';
  41. elseif( $i > 0 && $digits[1] == 1 )
  42. $string .= $nastki[$digits[0]] . ' ';
  43. if( $digits[0] > 0 && $digits[1] != 1 )
  44. $string .= $jednosci[$digits[0]] . ' ';
  45.  
  46. return $string;
  47. }
  48. ?>


Ponieważ pisałem ją naprędce w pracy (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) może zawierać błędy, jeżeli takowe ktos znajdzie, bardzo proszę o info w tym wątku.

Aha funkcja działa dla przedziału liczbowego od 1 - 999999.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
doggy1988
post
Post #2





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 16.09.2009

Ostrzeżenie: (0%)
-----


moje rozwiązanie (IMG:style_emoticons/default/wink.gif)

aktualna wersja z bloga
  1. <?php
  2.  
  3. /**
  4.  * Number to words converter (polish language)
  5.  *
  6.  * @author programista.it
  7.  * @package Number2Words
  8.  */
  9.  
  10. class Number2WordsPL {
  11.  
  12. /**
  13.   * Converts number to words in polish language
  14.   *
  15.   * @param integer $number The number
  16.   *
  17.   * @return string Words representation
  18.   */
  19. static function convert($number) {
  20. // check if number is negative
  21. $negative = false;
  22. if($number < 0) {
  23. $negative = true;
  24. $number = abs($number); // turn to positive
  25. }
  26. if($number == 0) { // if zero return zero
  27. return 'zero';
  28. }
  29. $i = -1; // our numberMap key
  30. $result = '';
  31. while($number >= 1) {
  32. $token = $number % 1000; // get 3 digits
  33. $number = ($number - $token) / 1000; // cut the number
  34. if($i >= 0) { // if numberMap key is greater than equal thousands
  35. list($first, $second, $third) = self::$numberMap[$i]; // get plural values from numberMap
  36. $pluralize = self::pluralize($token, $first, $second, $third); // pluralize
  37. } else {
  38. $pluralize = '';
  39. }
  40. if($token != 0) { // for zero we don't write anything to output
  41. $hundredsOf = self::hundredsOf($token) . ' '; // convert 3 digit token
  42. $result = $hundredsOf . $pluralize . ' ' . $result ; // add to output string
  43. }
  44. $i++;
  45. }
  46. return trim($negative ? 'minus ' . $result : $result);
  47. }
  48.  
  49. /**
  50.   * Pluralize the word
  51.   *
  52.   * @param integer $number The number with word would be pluralized
  53.   * @param string $first The first variety of the word
  54.   * @param string $second The second variety of the word
  55.   * @param string $third The third variety of the word
  56.   *
  57.   * @return string Pluralized word
  58.   */
  59. static function pluralize($number, $first, $second, $third) {
  60. $number = abs($number); // get absolute value, for negative numbers algoritm is the same
  61. if($number > 20) { // if number is greater than 20
  62. $number %= 10; // get the last digit
  63. if($number == 1) { // for 21, 31, 41, ... result is the same as for 0
  64. $number--;
  65. }
  66. }
  67. if($number == 1) { // 1 - first case
  68. return $first;
  69. } else if($number >= 2 && $number <= 4) { // 2,3,4 - second case
  70. return $second;
  71. } else { // 0,6,7,8,9 - third case
  72. return $third;
  73. }
  74. }
  75.  
  76. protected static $numberMap = array(
  77. array('tysiąc', 'tysiące', 'tysięcy'),
  78. array('milion', 'miliony', 'milionów'),
  79. array('miliard', 'miliardy', 'miliardów'),
  80. array('bilion', 'biliony', 'bilionów'),
  81. array('biliard', 'biliardy', 'biliardów'),
  82. array('trylion', 'tryliony', 'trylionów'),
  83. array('tryliard', 'tryliardy', 'tryliardów')
  84. );
  85.  
  86. protected static $ones = array(
  87. 'jeden', 'dwa', 'trzy',
  88. 'cztery', 'pięć', 'sześć',
  89. 'siedem', 'osiem', 'dziewięć'
  90. );
  91.  
  92. protected static $tens = array(
  93. 'dziesięć', 'dwadzieścia', 'trzydzieści',
  94. 'czterdzieści', 'pięćdziesiąt', 'sześćdziesiąt',
  95. 'siedemdziesiąt', 'osiemdziesiąt', 'dziewięćdziesiąt'
  96. );
  97.  
  98. protected static $specialTens = array(
  99. 'jedenaście', 'dwanaście', 'trzynaście',
  100. 'czternaście', 'piętnaście', 'szesnaście',
  101. 'siedemnaście', 'osiemnaście', 'dziewiętnaście'
  102. );
  103.  
  104. protected static $hundreds = array(
  105. 'sto', 'dwieście', 'trzysta',
  106. 'czterysta', 'pięćset', 'sześćset',
  107. 'siedemset', 'osiemset', 'dziewięćset'
  108. );
  109.  
  110. protected static function hundredsOf($number) {
  111. $ones = $number % 10;
  112. $tens = (($number % 100) - $ones);
  113. $hundreds = ($number - $tens - $ones) / 100;
  114. $tens /= 10;
  115.  
  116. $result = '';
  117. if($hundreds != 0) {
  118. $result .= self::$hundreds[$hundreds - 1] . ' ';
  119. }
  120. if($tens == 1 && $ones != 0) {
  121. $result .= self::$specialTens[$ones - 1] . ' ';
  122. } else {
  123. if($tens != 0) {
  124. $result .= self::$tens[$tens - 1] . ' ';
  125. }
  126. if($ones != 0) {
  127. $result .= self::$ones[$ones - 1] . ' ';
  128. }
  129. }
  130.  
  131. return trim($result);
  132. }
  133. }


  1. <?php echo Number2WordsPL::convert(1029007438124892645);


a jak chcecie do faktur, to ma też pluralize, np.
  1. <?php Number2WordsPL::pluralize(13, 'grosz', 'grosze', 'groszy');


Ten post edytował doggy1988 2.02.2012, 19:13:36
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 11.10.2025 - 01:55