Skip to content

Instantly share code, notes, and snippets.

@skowron-line
Last active June 30, 2017 19:47
Show Gist options
  • Save skowron-line/95d863ac43560bfd256ff1b208c7daa7 to your computer and use it in GitHub Desktop.
Save skowron-line/95d863ac43560bfd256ff1b208c7daa7 to your computer and use it in GitHub Desktop.
Money format, and money amount in words
<?php
/**
* @author Krzysztof Skaradziński <skaradzinski.krzysztof@gmail.com>
*/
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