The sFire\Utils\Number object is a handy utility that allows you to manipulate numbers even when they are in a String/sentence. For example if you have a user input from a form where a number has been send with a currency, you can easily ceil, floor or strip the number for your convenient wihtout alternate the other content.
Note: You may set and retrieve multiple numbers in and from a String.
In this section we will handle:
There are two ways to set a Number/String. You can give a String, Float or Integer in the constructor:
$str = 'Amount is: $5.00';
$number = new Number($str);
Or you may use the set method which accepts the same input types as above.
$str = 'Amount is: $5.00';
$number = new Number();
$number -> set($str);
Round fractions up.
$str = 'Amount is: $4.3';
$number = new Number($str);
$output = $number -> ceil();
echo $output; //Will output "Amount is: $5"
Round fractions down.
$str = 'Amount is: $4.3';
$number = new Number($str);
$output = $number -> floor();
echo $output; //Will output "Amount is: $4"
Rounds a number with an optional first and only parameter which represent the precision of decimal digits to round to.
$str = 'Amount is: $4.3';
$number = new Number($str);
$number -> round(); //Will output "Amount is: $4"
$str = 'Amount is: $4.6';
$number = new Number($str);
$number -> round(); //Will output "Amount is: $5"
$str = 'Amount is: $1.95583';
$number = new Number($str);
$number -> round(2); //Will output "Amount is: $1.96"
The toFixed method converts a number into a String, keeping a specified number of decimals.
$str = 'Amount is: $4.3';
$number = new Number($str);
$output = $number -> toFixed(2);
echo $output; //Will output "Amount is: $4.30"
You can use two methods to extract numbers from a String.
The strip methods strips all numbers from String and returns these in an Array.
$str = 'Amount is $4.3 and $5.52';
$numbers = new Number($str)
$output = $numbers -> strip(2);
print_r($output); //Will output Array(4.3, 5.52)
The "val" method strips all the numbers from a String and returns the value with an optional index to retrieve. You may give an Integer as first and only parameter to return the index of the value to return. If the index does not exists, it returns NULL.
$str = 'Amount is $4.3 and $5.52';
$numbers = new Number($str)
echo $numbers -> val(); //Will output 4.3
echo $numbers -> val(1); //Will output 5.52
To format numbers you can use the format method. This method accepts four optional parameters.
$number -> format([integer $decimals, string $point, string $thousands_separator, string $currency]);
$str = 'Amount is: 52852.3';
$number = new Number($str)
$output = $number -> format(2, '.', ',', '$');
echo $output; //Will output "Amount is: $52.852,30"
sFire\Utils\Number lets you chain multiple methods for a result you are looking for. For example, if you want to round the Number and then format it, you can do:
$str = 'Amount is: $4.3';
$number = new Number($str);
$number -> round() -> toFixed(2); //Will output "Amount is: $4.00"
You may also combine other methods like the format method for example.