Calculate (ADD) all Numbers for each page->field to echo it

Hey,

I have been wondering,

I tried to add a field from all pages which has a basic number but have had trouble with it…

my code was about like that:

 <?php foreach ( $pages->find('subpage')->children()->visible() as $subpage): ?>
     <?php $sum = $sum + $subpage->fieldwithnumber() ?>
<?php endforeach ?>

<?php echo $sum; ?>

The result has been that it’ll add a “one” for each subpage…

When just giving it a tried to echo’ing $subpage->fieldwithnumber() without any calculation it’ll show each number just right. But the calculation itself has failed.

Are there limits such as whole numbers, as well as something like “8.1234”

Any hints?

Cheers.

I think you need to convert the field number into an integer:

<?php foreach ( $pages->find('subpage')->children()->visible() as $subpage): ?>
  <?php $sum += $subpage->fieldwithnumber()->int() ?>
<?php endforeach ?>

<?php echo $sum; ?>

Thanks for this code.
I use this for a field with number like this 00:20 (hour and min).
It works but the calculated number from all fields is rounded.