String to float conversion not working php - php

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

Related

PHP Float conversion fails, returns 1

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.

numberformat error with bigs numbers in php

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.

Need to cast string value representing a 0.XXXX floats to actual number value in PHP

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;

Get value in php double data type format

$td = 4.0;
echo $td;
The output is 4;
But I want real number (4.0) in double data type;
First, not to be nitpicky, but PHP doesn't have the type you want*. When you do $td = 4.0; you have created a float.
If you inspect it with var_dump($td);, you'll see: float 4. Since there isn't really a concept of significant figures here, the zero after the decimal is not relevant to the stored value.
Second, when you do echo $td;, PHP will output the string representation of float 4. Again, without somehow specifying that you want to display n decimal places, PHP will omit any trailing zeroes. For another example, if you did this
$td = 4.00010000;
echo $td;
You'd see
4.0001
This is why the other answers/comments are guiding you toward a formatting solution. Because what you're really needing to do is not to change the type of the variable, because it's already stored in an appropriate type. You just need to specify how it should be displayed when it's converted to a string. There are different ways to do that. If you use printf, you can specify a number of decimal places to display. Here's how you make it show one, for example:
printf('%.1f', $td);
The '%.1f' is a format string. The .1 part is what tells it to show one decimal place. But you aren't changing the type. It's just output formatting.
*Here's a list of PHP's native types. And I was sort of mistaken, it does indicate that float is aka double.
You can use printf
echo printf("%f\n", $td);
Check this out -> string number_format ( float $number [, int $decimals = 0 ] )
Doc: http://php.net/manual/en/function.number-format.php
$td = 4.0;
echo number_format($td,1);
this will spit out 4.0 the "1" is the number of decimals you want in the number

PHP - how to get consistent output for API of values - floats with specific number of decimal places

My API request currently returns JSON in the following format:
[
{
"date":"2016-08-11",
"voltage":0.1,
"current":0.01,
"power":0,
"energy": null,
}
]
I'm trying to ensure consistency of data output by forcing the voltage, current and power values to be three decimal places, type float. I've tried using number_format() but the output are type strings, which I do not want. Is there a way of doing this, i.e. outputting 0 as a float 0.000 rather than staying an integer? Also, when no values are present I need to convert null into 0.000, is that possible without casting to strings?
The question PHP float with 2 decimal places: .00 doesn't explain (in layman's terms) why this is not possible in PHP.
echo floatval(number_format('0.001', 3)); // output: float 0.001
But unfortunately strings containing '0.000' or null after type casting will produce 0.
echo (float)number_format('0.000', 3); // output: 0
echo floatval(number_format('0.000', 3)); // output: 0
echo floatval(number_format(null, 3)); // output: 0
"...bytes with value 0 (“NUL bytes”) are allowed anywhere in the string (however, a few functions, said in this manual not to be “binary safe”, may hand off the strings to libraries that ignore data after a NUL byte.)" Details of the String Type
You may handle output at the presentation layer instead.

Categories