문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top