Last active
June 30, 2017 19:47
-
-
Save skowron-line/95d863ac43560bfd256ff1b208c7daa7 to your computer and use it in GitHub Desktop.
Money format, and money amount in words
This file contains hidden or 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 | |
/** | |
* @author Krzysztof Skaradziński <[email protected]> | |
*/ | |
class Money | |
{ | |
/** | |
* @param $value | |
* | |
* @return mixed | |
*/ | |
public function format($value, $divider = 10) | |
{ | |
static $formatter = null; | |
if (null === $formatter) { | |
$formatter = new \NumberFormatter('pl_PL', \NumberFormatter::CURRENCY); | |
} | |
$value = $value / $divider; | |
$value = round($value, 2); | |
return str_replace('zł', '', $formatter->format($value)); | |
} | |
/** | |
* @param $value | |
* | |
* @return string | |
*/ | |
public function inWords($value) | |
{ | |
static $formatter = null; | |
if (null === $formatter) { | |
$formatter = new \NumberFormatter('pl_PL', \NumberFormatter::SPELLOUT); | |
} | |
$value = str_replace('.', ',', $value); | |
if (false === stristr($value, ',')) { | |
return sprintf('%s zł', $formatter->format($value)); | |
} | |
list($zl, $gr) = explode(',', $value); | |
$gr = str_pad($gr, 2, 0, STR_PAD_RIGHT); | |
return sprintf('%s zł, %s gr', $formatter->format($zl), $formatter->format($gr)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment