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

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • Can you convert string to double?
  • How do you double in PHP?
  • How do I convert a string to a number in PHP?
  • Does PHP have double?

thanks

asked Mar 29, 2010 at 17:50

6

Just use floatval().

E.g.:

$var = '122.34343';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343

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.

answered Mar 29, 2010 at 17:53

Hướng dẫn php string to double

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.

$s = '1234.13';
$double = bcadd($s,'0',2);

PHP: bcadd

answered Mar 29, 2010 at 17:56

2

Use doubleval(). But be very careful about using decimals in financial transactions, and validate that user input very carefully.

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

  • Read
  • Discuss
  • 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:

    $num = "1000.314";

    echo number_format($num), "\n";

    echo number_format($num, 2);

    ?>

    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:

    $num = "1000.314";

    echo (int)$num, "\n";

    echo (float)$num, "\n";

    echo (double)$num;

    ?>

    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:

    $num = "1000.314";

    echo intval($num), "\n";

    echo floatval($num);

    ?>

    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.

    $num = "1000.314";

    echo $num + 0, "\n";

    echo $num + 0.0, "\n";

    echo $num + 0.1;

    ?>

    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)