PHP - convert string to float

I need to convert String to Float.

I will get the lines as follows:

$string = "1.70 m"; $string2 = "2.445 m"; 

How can you easily convert these lines to:

 $float1 = 1.70; $float2 = 2.445; 

Can someone give me some clues?

Best wishes,

+4
source share
4 answers

These are floats , not integers . Integers do not have decimal points.

To answer your question, you can simply typecast strings directly, the conversion will disable units, since they are not numeric characters:

 $string = "1.70 m"; $float = (float) $string; 
+9
source

you can get it

 echo (float)array_shift(implode(' ', $string)); 

Update:

 echo (float) $string; 
+3
source

The easiest way to do this is probably using the floatval () function:

http://ca.php.net/manual/en/function.floatval.php

For exmaple:

 floatval("1.70 m"); 

gives you:

1.7

+2
source
 $integer = intval($string); 

Enjoy: D

-1
source

Source: https://habr.com/ru/post/1340457/


All Articles