Convert string to double - is this possible?

It's just interesting in php if you could convert a string to double. I use a financial web service that provides a price as a string. I really need to treat this as a double, and I was wondering how I converted it.

thank

+45
php
Mar 29 '10 at 17:50
source share
4 answers

Just use floatval() .

eg:.

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

And if you are wondering doubleval() , this is just an alias for floatval() .

And, as others say, in a financial application, float values โ€‹โ€‹are crucial because they are not accurate enough. For example. adding two floats can lead to something like 12.30000000001 , and this error can propagate.

+84
Mar 29 '10 at 17:53
source share

For arbitrary precision mathematics, PHP offers a Binary Calculator that supports numbers of any size and precision, represented as strings.

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

PHP: bcadd

+13
Mar 29 '10 at 17:56
source share

Use doubleval() . But be very careful about using decimal places in financial transactions and carefully check that the user enters very much .

+2
Mar 29 '10 at 17:54
source share

Why is floatval the best option for financial comparison data? The bc functions accurately believe strings in real numbers.

0
Jul 25 '12 at 5:13
source share



All Articles