Home Forums General Issues 25.654628 to 25.65 Decimal Update


25.654628 to 25.65 Decimal Update

  • I needed to show long decimal latitude and longitude values (e.g. 25.654628547) to 2dps on the front end. I have 6 fields and more than 400 posts which make it difficult to edit one by one.

    	$output = get_field('latitude_metric');
    	$latitude = round($output, 2);
    	echo $latitude;

    I found this code on this forum. When I copy paste this to my functions.php the website doesn’t work. Is there a practical way to achieve this? I don’t know much about PHP.

    Theme: Astra
    Theme Builder: Elementor Pro

  • I think something like that should work:

    function my_acf_format_value( $value, $post_id, $field ) {
    	$value = round($value, 2);
    	echo $value;
    // Apply to fields named "my_field_name".
    add_filter('acf/format_value/name=my_field_name', 'my_acf_format_value', 10, 3);

    The filter “acf/format_value” give the ability to modify the value of a specific field (here by field’s name).

    Hope this helps 😉

  • Thank you, this was helpful.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.