TomPAP
TomPAP

Reputation: 259

float number in php to javascript

I am trying to set a point (lat/lon) with google API I was doing something like

new LatLon(<?php echo (float)$lat ?>,<?php echo(float)$lng)?>);

But I discovered that on some servers, this was not working because

echo (float)1.1; ===== > display 1,1 (comma is the french separator for decimal)

Is that normal that the "echo" is not returning 1.1 ?? Is this something new in recent php version ?

should the correct solution be :

<?php echo json_encode((float)$lat)?>

Upvotes: 1

Views: 1523

Answers (2)

kushyar
kushyar

Reputation: 1191

You can use the floatval function to achieve that

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

you can also use number_format to specify the number of digits after the decimal point as below:

number_format($float_value_of_var,2); // 122.34

edit: my bad, dystroy above is right about setting the locale

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

Yes, echo uses the locale. Put this in your code before :

setlocale(LC_NUMERIC, 'en_US');

Depending on your needs, you might also want to simply set

setlocale(LC_ALL, 'en_US');

Upvotes: 2

Related Questions