Convert string to number

I would like to integrate an OSM map into my page to generate the bounding box I have to convert a number field value into a number.

I tried the following but that output is 0.

echo $lon = (int)(string)$page->location_lon();

How should I do it right?

The link target in this article no longer exists.

Would it be possible to leave the old docs online for a while?

greetings perry

The K2 docs are still online :wink: : Kirby 2 Docs

@bvdputte good to know :grinning: but the links into the forum articles -> 404.

I get 0 because the string is e.g. 7,230456 ?

You don’t want to convert “7,xxxx” to integer, that doesn’t make sense. Also, if you have a string with a comma, you would have to replace that with a dot or use number_format() to convert.

Now I understand.

echo $lon = (float)(string)$page->location_lon();

thank you for your help.

There is also ->float(): https://k2.getkirby.com/docs/cheatsheet/field-methods/float :wink:

@distantnative: I think float() doesn’t really work with a decimal with a comma, does it? At least I tested this earlier today in K3, and I think the behavior is not different in K2.

If this works, ->float()(using floatval internally) should as well, don’t you think?

With

<?= $page->lon()->float() ?>

I only get a “7” from “7,123456” :thinking: Works fine if the value is “7.123456”

As texnixe said, $page->location_lon()->float() won’t work if location_lon() contains a string with a comma.
Exactly the same happens when casting to (float).

Luckily the Kirby number field saves numbers with a dot, and not with a comma :slight_smile:

yes! thank you for your help.

In the imput field (panel) a point is converted to the comma, so I thought it was also saved as a comma…

my osm snippet maybe someone needs it

<?php 

$lon = (float)(string)$page->location_lon();
$lat = (float)(string)$page->location_lat();
//bounding box
//left
$bb_max_lon = $lon - 4.15; 
//bottom 
$bb_min_lat = $lat - 4.15;
//right
$bb_min_lon = $lon + 4.15;
//top
$bb_max_lat = $lat + 4.15;  


echo "<iframe src='https://www.openstreetmap.org/export/embed.html?bbox=".$bb_max_lon.",".$bb_min_lat.",".$bb_min_lon.",".$bb_max_lat."&amp;layer=mapnik&amp;&amp;marker=".$lat .",".$lon."'></iframe>";
?>