From 1b10f45a7df590b77f09efd5d551cedb921af9ce Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Thu, 13 Sep 2018 15:55:42 +0200 Subject: [PATCH] Added helper function 'formatAmount' to auto format floats top german format. --- app/Http/helpers.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Http/helpers.php b/app/Http/helpers.php index 505ce94..8d8cf10 100644 --- a/app/Http/helpers.php +++ b/app/Http/helpers.php @@ -10,7 +10,7 @@ function rand_bool($prob = 50) { if ($prob < 0 or $prob > 100) { throw new InvalidArgumentException('The value for the probability '. - ' must be in a range from 0 to 100.'); + 'must be in a range from 0 to 100.'); } else if (!is_int($prob)) { throw new InvalidArgumentException('rand_bool only accepts integers.'); } @@ -19,3 +19,18 @@ function rand_bool($prob = 50) { return $rand <= $prob; } + +/* + * Return the formatted string representation of a floatng point number. + * + * The decimal point is a ',', the thousands seperator ist set to '.'. + * The number of decimals is 2 by default but can be set by the $decimals + * argument. + * + * @param integer $decimals Sets the number of decimal points. + * + * @return string + */ +function formatAmount(float $amount, $decimals = 2) { + return number_format($amount, $decimals, ',', '.'); +}