Frage

I'm building a web application that is handling some money/prices that may change from time to time. To facilitate this, I have a config.php which is required and includes some definitions:

define('PRODUCT_PRICE', 2000.00);

define('PRODUCT_PRICE_WITH_GST', PRODUCT_PRICE * 1.1);

When it comes time for the prospect to enter the checkout process, I display the price as follows:

<?php echo(PRODUCT_PRICE_WITH_GST); ?>

I was expecting this to print as 2200.00 but rather it's dropping off the decimal points and displaying as 2200.

What's the best practice way to handle this?

War es hilfreich?

Lösung

I would use number_format: http://php.net/number_format

if you var_dump() your constant, you see its internal representation is a double, it is only a manner of how you show it.

Andere Tipps

Take a look at the functions sprintf and printf for various kinds of string formatting. Your particular problem has the following solution:

printf("%01.2f", PRODUCT_PRICE_WITH_GST);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top