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.
<?php
$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).
https://www.advancedcustomfields.com/resources/acf-format_value/
Hope this helps 😉
Thank you, this was helpful.