Hướng dẫn php string to double
Just wondering in php, if it was possible to convert a string to a double. I am using a financial web service which provides a price as a string. I really need to process this as a double and was wondering how i would convert it Show Nội dung chính
thanks asked Mar 29, 2010 at 17:50 6 Just use E.g.:
And in case you wonder And as the other say, in a financial application, float values are critical as these are not precise enough. E.g. adding two floats could result in something like answered Mar 29, 2010 at 17:53 Felix KlingFelix Kling 768k171 gold badges1068 silver badges1114 bronze badges 1 For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.
PHP: bcadd answered Mar 29, 2010 at 17:56 2 Use answered Mar 29, 2010 at 17:54 JSBձոգչJSBձոգչ 39.9k17 gold badges98 silver badges166 bronze badges Why is floatval the best option for financial comparison data? bc functions only accurately turn strings into real numbers. answered Jul 25, 2012 at 5:13 XenlandXenland 5101 gold badge6 silver badges18 bronze badges Not the answer you're looking for? Browse other questions tagged php or ask your own question.View Discussion Improve Article Save Article View Discussion Improve Article Save Article Strings in PHP can be converted to numbers (float / int / double) very easily. In most use cases, it won’t be required since PHP does implicit type conversion. There are many methods to convert string into number in PHP some of them are discussed below: Method 1: Using number_format() Function. The number_format() function is used to convert string into a number. It returns the formatted number on success otherwise it gives E_WARNING on failure. Example:
Method 2: Using type casting: Typecasting can directly convert a string into float, double or integer primitive type. This is the best way to convert a string into number without any function. Example:
Output: 1000 1000.314 1000.314 Method 3: Using intval() and floatval() Function. The intval() and floatval() functions can also be used to convert the string into its corresponding integer and float values respectively. Example:
Method 4: By adding 0 or by performing mathematical operations. The string number can also be converted into an integer or float by adding 0 with the string. In PHP, performing mathematical operations, the string is converted to an integer or float implicitly.
Output: 1000.314 1000.314 1000.414 PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Can you convert string to double?new Double(String s) We can convert String to Double object through it's constructor too. Also if we want double primitive type, then we can use doubleValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseDouble() or valueOf() methods. How do you double in PHP?Just use floatval() . And in case you wonder doubleval() is just an alias for floatval() . And as the other say, in a financial application, float values are critical as these are not precise enough. E.g. adding two floats could result in something like 12.30000000001 and this error could propagate. How do I convert a string to a number in PHP?To convert string to integer using PHP built-in function, intval(), provide the string as argument to the function. The function will return the integer value corresponding to the string content. $int_value = intval( $string ); Does PHP have double?PHP supports the following data types: String. Integer. Float (floating point numbers - also called double) |