Recent Posts

Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

How to solve: number_format() expects parameter 1 to be double, string given

This error is common when the function number_format recieves an empty string or a string with special characters. To solve this you can use floatval to make the conversion to float.

number_format( floatval ($string_variable), 2); 

How to format number as money with JavaScript

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);

var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return '$ ' + intPart + DecimalSeparator + decPart;

Format a number with two decimal places in PHP

To accomplish this you can use number_format()

return number_format((float)$number, 2, '.', '');
Example:

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00
This function return a string