Who Stole My Zeros?!

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 22, 2008   (RSS)

By Perchpole - May 22, 2008

Here's a quickie for the mathematicians out there...

I'm using a record to perform a calculation that generates a simple product shipping cost. The calculation is the product weight multiplied by a fixed number. Something like this:

<?php echo $record['weight'] * 5 ?>




Although this works fine in practice (and can be used in systems like Paypal, etc.) the figures don't look right.

For example, instead of £2.50 what you see is £2.5!

Instead of £7.00 all you get is £7.

Simple question: How do I rework the code to display the missing zeros?!

:0o

Perch

Re: [Perchpole] Who Stole My Zeros?!

By Dave - May 22, 2008

Hi Perch,

We're going to have to use a PHP function for this.

instead of this: <?php echo $record['weight'] * 5 ?>

try this: <?php echo number_format($record['weight'] * 5, 2) ?>

The ", 2" tells it to _always_ show 2 decimals, even then they're 00. This will also add thousands separators. So one million will display like this: 1,000,000.00. If it's a problem we can have it not add those. Let me know what works best for you.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com