DroidMatt
DroidMatt

Reputation: 183

How to convert a String type to Float type in MySQL/PHP?

I have a string like this:

$foo = "1.3223";

How do I convert $foo into a float?

Upvotes: 0

Views: 1567

Answers (5)

MaxSan
MaxSan

Reputation: 318

Remove the speech marks and PHP should convert into a float. or cast it.

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

Try casting :

$yourFloat = (float) $foo;

Upvotes: 4

mas-designs
mas-designs

Reputation: 7536

This would do it:

<?php
$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>

Upvotes: 1

hsz
hsz

Reputation: 152206

Cast it using (float):

$foo = (float) "1.3223";

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60516

https://www.php.net/floatval

$foo = floatval($foo);

Upvotes: 3

Related Questions