This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

Introduction

Starting from version 5.6.2.1, concrete5 offers new functions for formatting numbers.

Indeed, the NumberHelper has indeed been greatly improved. To use it, it's enough to use this line:

$nh = Loader::helper('number');

When not specified, in this document we'll assume the en_US locale, but the same rules apply for any locale.

Formatting: the format() method

If you want to format a number, respecting the current locale, we have the format method.

Let's do an example: echo $nh->format(1234.5678);

  • If the current locale is en_US you'll have 1,234.5678.
  • If the current locale is it_IT you'll have 1.234,5678.

Nice, isn't it?

We have also the possibility to specify the precision as the second parameter:

With

echo $nh->format(1234.5678, 2);

for en_US you'll have 1,234.57

And what about trying to format invalid values (for instance: $nh->format('Not a number', 2))? format() will simply return back the same value you passed, without modifying it.

Parsing localized values: the unformat() method

Let's suppose that you want to allow users to insert numbers with their own locale formatting. In this case you'd need to normalize those values.

For instance, with en_US locale you may receive from users the input '1,234.5678'.

Obviously, before using or saving it as a number you have to standardize it. For this you can use the unformat() method:

echo $nh->unformat('1,234.5678');

That results in 1234.5678.

And what happens if the user inserts an invalid value? unformat() will return NULL.

var_dump($nh->unformat('This is not a number'));

That results in NULL. The same happens also for empty strings passed to unformat().

unformat() accepts two more parameters:

  • a boolean value that tells unformat() to strip out spaces and newlines found at the beginning and at the end of the strings to be un-formatted. This parameter (whose default value is true) allows parsing the string ' 123' without problems even if it contains an initial space
  • a number value that represents the maximum precision that you want. So, for instance if you want to be sure to receive an integer value you can write $nh->unformat('6.7', true, 0) to obtain 6. A value of NULL (default) for the precision value means that you accept the precision inserted by the user, the numerical value won't be touched.

Verifying formatted values: isNumber() and isInteger()

Even if unformat() will return NULL it it receives invalid formatted strings, you may want to check users input before proceeding. For this you can use the isNumber() and isInteger() methods:

  • $nh->isNumber('12,345.67') will give you true
  • $nh->isInteger('12,345.67') will give you false

Huge numbers

The format(), unformat(), isNumber() and isInteger() support huge numbers represented as strings. For instance:

echo $nh->format('12345679801234567980123456798012345679801234567980.987654321', 2);

will result in 12,345,679,801,234,567,980,123,456,798,012,345,679,801,234,567,980.99

Loading Conversation