Skip to content

Instantly share code, notes, and snippets.

@gskema
Created February 4, 2016 12:53
Show Gist options
  • Save gskema/b8893f4ee36a303f05be to your computer and use it in GitHub Desktop.
Save gskema/b8893f4ee36a303f05be to your computer and use it in GitHub Desktop.
Integer number in Lithuanian words
<?php
/**
* Converts an integer number to a word in Lithuanian language.
*
* @param int $number Integer number to be converted into words
* @param array|bool $units Units array like array(0 => šimtas, 1 => šimtai, 2 => šimtų);
* @param bool $one If false, word "vienas" is omitted
* @return string
*/
function numberToLithuanianWord($number, $units = false, $one = false)
{
$words = array(
0 => array('nulis'),
1 => array('vienas'),
2 => array('du'),
3 => array('trys'),
4 => array('keturi'),
5 => array('penki'),
6 => array('šeši'),
7 => array('septyni'),
8 => array('aštuoni'),
9 => array('devyni'),
10 => array('dešimt'),
11 => array('vienuolika'),
12 => array('dvylika'),
13 => array('trylika'),
14 => array('keturiolika'),
15 => array('penkiolika'),
16 => array('šešiolika'),
17 => array('septyniolika'),
18 => array('aštuoniolika'),
19 => array('devyniolika'),
20 => array('dvidešimt'),
30 => array('trisdešimt'),
40 => array('keturiasdešimt'),
50 => array('penkiasdešimt'),
60 => array('šešiasdešimt'),
70 => array('septyniasdešimt'),
80 => array('aštuoniasdešimt'),
90 => array('devyniasdešimt'),
100 => array('šimtas', 'šimtai'),
1000 => array('tūkstantis', 'tūkstančiai', 'tūkstančių'),
1000000 => array('milijonas', 'milijonai', 'milijonų'),
1000000000 => array('milijardas', 'milijardai', 'milijardų'),
);
// If base exists, then make a string: "X of base units"
$bases = array(1, 100, 1000, 1000000, 1000000000);
// Convert number to true integer
$num = (int)$number;
// Start building word
$word = '';
if (isset($words[$num])) {
// Simple cases
$word = $words[$num][0];
} elseif ($num < 100) {
// Exception for 10s
$remainder = $num % 10;
$word = self::numberToLithuanianWord($num - $remainder).' '.self::numberToLithuanianWord($remainder);
} else {
// Get highest power of 10
$pow = (int)floor(log10($num));
while ($num > 0) {
$base = pow(10, $pow);
if (in_array($base, $bases)) {
// How many of units?
$how_many = (int)floor($num / $base);
// Skip "0 units", e.g. 1000001
if ($how_many != 0) {
// Reduce number for further processing
$num -= $how_many * $base;
// Do not append [1, 99] units of "1"
if ($base == 1) {
$word .= ' '.self::numberToLithuanianWord($how_many);
} elseif (!$one && $how_many == 1) {
$word .= ' '.$words[$base][0];
} else {
$word .= sprintf(' %s', self::numberToLithuanianWord($how_many, $words[$base]));
}
}
}
$pow--;
}
// Restore original number
$num = (int)$number;
}
// Append units in correct form
if (is_array($units)) {
if ($num % 10 == 0 || ($num % 100 > 10 && $num % 100 < 20)) {
$word .= isset($units[2]) ? ' '.$units[2] : ''; // e.g. tūkstančių
} elseif ($num % 10 == 1) {
$word .= isset($units[0]) ? ' '.$units[0] : ''; // e.g. tūkstantis
} else {
$word .= isset($units[1]) ? ' '.$units[1] : ''; // e.g. tūkstančiai
}
}
// Is negative ?
return ($num < 0 ? 'Minus ' : '').trim($word);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment