bri0987 Posted October 23, 2007 Share Posted October 23, 2007 I have a variable that is set by a "Double field" in mysql. For example: In the database the price of a component is set to 45.50 If I echo out the var it works fine because of the cents. BUT - The problem is that when I do math on the var with another number (for example 45.50 + 54.50 ) The var will echo 100 I need the var to echo 100.00 IF there are no cents (or .00) I need the cents to display. Is there a way to force this. How can I do this. Any ideas, let me know. Thanks. BRI Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/ Share on other sites More sharing options...
teng84 Posted October 23, 2007 Share Posted October 23, 2007 <?php $number = 123.10; $txt = sprintf("With 2 decimals: %1\$.2f",$number); echo $txt; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/#findComment-376587 Share on other sites More sharing options...
roopurt18 Posted October 23, 2007 Share Posted October 23, 2007 There is also number_format() Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/#findComment-376588 Share on other sites More sharing options...
rajivgonsalves Posted October 24, 2007 Share Posted October 24, 2007 You can use the bcadd function following is an example $a = "45.50"; $b = "54.50"; print $a+b; //returns 100 print bcadd($a,$b,2); // returns 100.00 Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/#findComment-376874 Share on other sites More sharing options...
clearstatcache Posted October 24, 2007 Share Posted October 24, 2007 if ur doing ur addition in a query u may use the format function.... select format((num1 + num2), 2) as sum from tablename Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/#findComment-376879 Share on other sites More sharing options...
bri0987 Posted October 24, 2007 Author Share Posted October 24, 2007 [sOLVED] number_format() <<< This was the best way to do what I needed. Thank all! Quote Link to comment https://forums.phpfreaks.com/topic/74512-solved-php-string-trouble-hmmmmm/#findComment-377005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.