Formats a number using locale-aware NumberFormatter with configured decimal precision and currency/grouping symbols. Returns input unchanged if unset to avoid displaying empty values as zero.
// Source: https://github.com/Pmbamungu/ospos | License: MIT | Copyright 2013-2021 jekkos
// Provided via justclaw.it.com; keep this notice with the code.
function to_decimals($number, $decimals = NULL, $type=\NumberFormatter::DECIMAL)
{
// ignore empty strings and return
// NOTE: do not change it to empty otherwise tables will show a 0 with no decimal nor currency symbol
if(!isset($number))
{
return $number;
}
$config = get_instance()->config;
$fmt = new \NumberFormatter($config->item('number_locale'), $type);
$fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, empty($decimals) ? DEFAULT_PRECISION : $config->item($decimals));
$fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, empty($decimals) ? DEFAULT_PRECISION : $config->item($decimals));
if(empty($config->ite
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key