What is the PHP command that does something similar to intval()
, but for decimals?
Eg. I have string "33.66" and I want to convert it to decimal value before sending it to MSSQL.
You can use the float() function to convert any data type into a floating-point number. This method only accepts one parameter. If you do not pass any argument, then the method returns 0.0. If the input string contains an argument outside the range of floating-point value, OverflowError will be generated.
In Python, we can use float() to convert String to float. and we can use int() to convert String to an integer.
Strings can be converted to floating point numbers using different methods in Java. The different methods used to convert strings to float are: the valueOf() method, the parseFloat() method, Float class constructor, and the DecimalFormat class.
In Python, we can use str() to convert float to String.
How about floatval()
?
$f = floatval("33.66");
You can shave a few nanoseconds off of type conversions by using casting instead of a function call. But this is in the realm of micro-optimization, so don't worry about it unless you do millions of these operations per second.
$f = (float) "33.66";
I also recommend learning how to use sscanf()
because sometimes it's the most convenient solution.
list($f) = sscanf("33.66", "%f");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With