johnsmith153 Posted June 5, 2009 Share Posted June 5, 2009 I always mess around with this with some long winded process. But if I am adding amounts together - ie $1.02 $1.05 $2.33 etc.... and if when adding these values up I get something like 4.40 - then php will store as 4.4 How would I ensure it shows 4.40 without my long-winded script? ie I dont want to show the user: $4.4 $4.42 $3 I want them to see $4.40 $4.42 $3.00 Link to comment https://forums.phpfreaks.com/topic/161087-is-there-a-simple-way-to-convert-decimal-into-money/ Share on other sites More sharing options...
Philip Posted June 5, 2009 Share Posted June 5, 2009 sprintf() or for printing: printf() See example #8 on sprintf Link to comment https://forums.phpfreaks.com/topic/161087-is-there-a-simple-way-to-convert-decimal-into-money/#findComment-850066 Share on other sites More sharing options...
johnsmith153 Posted June 5, 2009 Author Share Posted June 5, 2009 $amt = sprintf("%01.2f", $amt); Great, thanks. Link to comment https://forums.phpfreaks.com/topic/161087-is-there-a-simple-way-to-convert-decimal-into-money/#findComment-850070 Share on other sites More sharing options...
Mark Baker Posted June 5, 2009 Share Posted June 5, 2009 or look at the money_format() function, depending on the version of PHP that you're running Note that it's also useful to hold all monetary values as integer cents rather than float dollars. That way, you're doing integer mathematics rather than floating point mathematics when summing up values or calculating tax, and you'll be less susceptible to rounding errors Link to comment https://forums.phpfreaks.com/topic/161087-is-there-a-simple-way-to-convert-decimal-into-money/#findComment-850118 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.