I have a function which returns a value of 1725.00 using the number_format(value,2). So, now when I convert it to float, it gives 1, same for int,intValue,floatValue. Even I tried multiplying with 100 to get the int value, but it gives an error of A non well formed numerical value. Does anyone know what is wrong here?
$balance = (float) currentBalance($user_id); // currentBalance gives a value of 1725, but (float) gives makes the value 1.
print_r($balance); die; //gives 1.
I'm using PHP 7.0+ and Laravel 5.8.
Your problem is that number_format returns a string with commas inserted for thousand separators, so the return value from your function is 1,725.00. When you try to cast this as a float PHP gets as far as the comma and says this is no longer a number and so returns 1.
If you need to have a formatted string returned by currentBalance, your best bet is to use
$balance = (float)str_replace(',', '', currentBalance($user_id));
Otherwise, replace the call to number_format with a call to round so that currentBalance returns a numeric value instead.
Related
I've a string value of the type "x.yy" which I want to convert to double/float.
I've tried floatval(), doubleval() and float and double type casting as well but it only gives me "X" as the number and ignores the part after decimal i.e ".yy".
$duration = "3.08";
echo (float)$duration; // gives 3 and ignores .08
echo floatval($duration); // same as above
expected result should be 3.08 with type float.
I've tried solutions already but didn't worked. Can anyone tell me how to get this working ?
Thanks
I am returning JSON from this array, but the value of total is in string format - not a float. Not sure what I am doing wrong here.
This is the array, which ends up having a string:
return [
'cart' => [
'total' => amount($cart->total)
]
]
And this is the amount helper method, where the issue is coming from:
function amount($money)
{
return number_format(floatval($money), 2);
}
I am expecting a float back from total, not a string.
number_format returns a string.
In your case, you might be passing a value such as 3.15 in which case a string would seem to be an odd output. But, if you passed a value like 311583249 you would see why this is the case: number_format(311583249) returns "311,583,249".
It returns a string every time for continuity. If it returned an integer when there's a whole number less than 1,000, or a a float when there's a partial number less than 1,000, then that would be more difficult to account for overall and not serve the exact purpose that number_format has - so a string is always returned by number_format.
What you might be looking for instead would be round(X, 2); this would return a float with the precision of 2. Or maybe you're looking for money_format('%i', X) - this also returns a string but formatted as if it were money.
That's normal. In this case, if you break down the types of everything you're executing like so:
var_dump(1000000.12);
var_dump(floatval(1000000.12));
var_dump(number_format(floatval(1000000.12), 2));
Then you will notice that number_format is the point at which it becomes a string, given the comma delimitation:
float(1000000.12)
float(1000000.12)
string(12) "1,000,000.12"
Also this may be useful for you as an alternative approach.
I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.
I'm developing a e-commerce and I have a problem when I want to format a number with number_format().
I have to set to my Stripe connection a number without decimals, so when I do all the calculations to have the final price of my shoppingcart I do:
$final_amount = number_format($final_amount, 2) * 100;
The result is a number that Stripe understands. I haven't got any problem with small numbers (like 970.25 or 1300.75 for example) but when I have a big amount like 15717.72 php throws the error "A non well formed numeric value encountered". I don't know if this is the problem, big numbers.
I've tried to parse previously $final_amount with floatval() and It didn't run either.
Someone knows the problem? thanks :)
A couple notes.
"A non well formed numeric value encountered" is a Notice, not an Error.
I don't believe 1300.75 works for you. The reason I don't believe this is you are only giving number_format two parameters. You are receiving that notice because number_format is formatting your number with a thousands separator ",".
$final_amount = number_format($final_amount, 2, ".", "") * 100;
should do the trick to remove that notice.
problem is not number_format() function but your calculation. You are multiplying a string with an integer. That does not work out so well.
$final_amount = number_format($final_amount * 100, 2);
Works just fine.
Update:
My conclusion was not completely correct. Multiplying an int with an string does work if the string is castable to int or float (see type juggling in PHP manual). But the string created by number_format() looks like this: "15,717.72". And thus cannot be cast to a number type.
So I am trying to cast a string value of ie: '0.0000143' to actual FLOAT or DECIMAL number value (so it must not be a string after conversion), but the actual number of 0.0000143 as it needs to be sent through some API call and the API requires it to be structured like that and not have an actual string value.
Can anyone help me achieve this (or is it actually even possible to do)?. Since I am out of ideas
To make things more clear, I have tried all the type casting possibilities there are in PHP (it is not working as I do not need something like 1.4E-5 returned to me, but the actual value like 0.0000143).
You can use Type casting in order to achieve that:
$string = '0.0000143';
$intValue = (int)$string;
$floatValue = (float)$string;