Last active
September 3, 2015 14:29
-
-
Save aliukevicius/3de8b88a005e8c534222 to your computer and use it in GitHub Desktop.
Display price in words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PriceInWords { | |
protected $moneyNames = ['euras', 'eurai', 'eurų']; | |
protected $centNames = ['centas', 'centai', 'centų']; | |
/** | |
* Convert price into words | |
* | |
* @param $price | |
* @return string | |
*/ | |
public function getPriceInWords($price) | |
{ | |
$price = sprintf('%01.2f', $price); | |
list($money, $cents) = explode('.', $price); | |
return $this->numberToWords($money) . ' ' | |
. $this->getName($money, $this->moneyNames) . ' ' | |
. $this->numberToWords($cents) . ' ' . $this->getName($cents, $this->centNames); | |
} | |
/** | |
* Get right number naming | |
* | |
* @param $number | |
* @param $names | |
* @return mixed | |
*/ | |
protected function getName($number, $names) | |
{ | |
$number = (int) $number; | |
if ($number == 0) { | |
return $names[2]; | |
} | |
$last = $number % 10; | |
$lastTwo = $number % 100; | |
if (($lastTwo > 10) && ($lastTwo < 20)) { | |
return $names[2]; | |
} else { | |
if ($last == 0) { | |
return $names[2]; | |
} elseif ($last == 1) { | |
return $names[0]; | |
} else { | |
return $names[1]; | |
} | |
} | |
} | |
/** | |
* Get number in words | |
* | |
* @param $number | |
* @return string | |
*/ | |
protected function numberToWords($number) | |
{ | |
$n0_9 = ['nulis', 'vienas', 'du', 'trys', 'keturi', 'penki', 'šeši', 'septyni', 'aštuoni', 'devyni']; | |
$n11_19 = [ | |
11 => 'vienuolika', | |
12 => 'dvylika', | |
13 => 'trylika', | |
14 => 'keturiolika', | |
15 => 'penkiolika', | |
16 => 'šešiolika', | |
17 => 'septyniolika', | |
18 => 'aštuoniolika', | |
19 => 'devyniolika' | |
]; | |
$tens = [ | |
1 => 'dešimt', | |
2 => 'dvidešimt', | |
3 => 'trisdešimt', | |
4 => 'keturiasdešimt', | |
5 => 'penkiasdešimt', | |
6 => 'šešiasdešimt', | |
7 => 'septyniasdešimt', | |
8 => 'aštuoniasdešimt', | |
9 => 'devyniasdešimt' | |
]; | |
$big = [ | |
2 => ["tūkstantis", "tūkstančiai", "tūkstančių"], | |
3 => ["milijonas", "milijonai", "milijonų"], | |
4 => ["milijardas", "milijardai", "milijardų"], | |
5 => ["trilijonas", "trilijonai", "trilijonų"], | |
]; | |
$return = ''; | |
$numberParts = $this->splitNumber($number); | |
$numberPartCount= count($numberParts); | |
foreach ($numberParts as $partIndex => $part) { | |
$num = (int) $part; | |
$bigNumberIndex = ($numberPartCount - $partIndex); | |
// get hundreds | |
if (($hNum = (int) ($num / 100)) > 0) { | |
if ($hNum == 1) { | |
$return .= ' šimtas'; | |
} else { | |
$return .= ' ' . $n0_9[$hNum] . ' šimtai'; | |
} | |
$num -= $hNum * 100; | |
} | |
// get numbers less then 100 | |
if ($num >= 11 && $num <= 19) { | |
$return .= ' ' . $n11_19[$num]; | |
} else if ($num > 0 && $num % 10 == 0) { | |
$tNum = (int) ($num / 10); | |
$return .= ' ' . $tens[$tNum]; | |
} else if ($num < 10) { | |
// instead of 'vienas tukstantis' we want to have 'tukstantis' etc. | |
if ($num != 1 || isset($big[$bigNumberIndex]) == false) { | |
if ($num != 0 || $numberPartCount == 1) { | |
$return .= ' ' . $n0_9[$num]; | |
} | |
} | |
} else if ($num < 100 && $num > 10) { | |
$tensNumber = (int) ($num / 10); | |
$singleNumber = $num - $tensNumber * 10; | |
$return .= ' ' . $tens[$tensNumber] . ' ' . $n0_9[$singleNumber]; | |
} | |
// get big number name | |
if (isset($big[$bigNumberIndex])) { | |
$return .= ' ' . $this->getName($num, $big[$bigNumberIndex]); | |
} | |
} | |
return trim($return); | |
} | |
/** | |
* Split number into parts of 3 digits | |
* | |
* @param $number | |
* @return array | |
*/ | |
protected function splitNumber($number) | |
{ | |
$parts = []; | |
while (strlen($number) > 0) { | |
$parts[] = substr($number, -3); | |
$number = substr($number, 0, -3); | |
} | |
$parts = array_reverse($parts); | |
return $parts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment