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 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; ?> 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() 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 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 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! 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
Archived
This topic is now archived and is closed to further replies.