WHO STOLE MY ZEROS? - Aug 3rd, 2010


User Perchpole was concerned by how zeros were displayed on a viewer page. He said: “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?!

Dave Edis from Interactive tools came to the rescue. He answered:

“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

So simple when you know how...



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service