slappadudle Posted May 5, 2009 Share Posted May 5, 2009 Running Joomla with Virtuemart and a payment module called NAB Transact. Problem - When the total is passed to the hosted payment gateway it displays incorrectly because anything over 999 loses its first digit! Eg. $1,234.56 prints as $234.56. Here's the code that fetches and prints the total: $total = $db->f("order_total"); echo number_format($total, 2, '.', ','); What am I doing wrong? This critical issue is preventing an imminent launch! Please help!!! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/ Share on other sites More sharing options...
kenrbnsn Posted May 5, 2009 Share Posted May 5, 2009 What's the input string look like? Ken Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826471 Share on other sites More sharing options...
slappadudle Posted May 5, 2009 Author Share Posted May 5, 2009 SQL reads: `order_total` decimal(15,5) NOT NULL default '0.00000' Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826476 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 If it's already formatted correctly, why format it again? o.O Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826492 Share on other sites More sharing options...
slappadudle Posted May 5, 2009 Author Share Posted May 5, 2009 Tried a few things with no success... Even removing the formatting code so it reads: $total = $db->f("order_total"); echo number_format($total); Still removes the first digit over 999. What should I do??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826518 Share on other sites More sharing options...
Daniel0 Posted May 5, 2009 Share Posted May 5, 2009 What's the input string look like? Ken Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826521 Share on other sites More sharing options...
slappadudle Posted May 5, 2009 Author Share Posted May 5, 2009 What's the input string look like? Ken Sorry guys but I don't know what an input string is! Please explain? Thanks alot for replying promptly, we appreciate your support!! Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826528 Share on other sites More sharing options...
Daniel0 Posted May 5, 2009 Share Posted May 5, 2009 The input string is the string your put into something, in this case the string you put into the function. Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826529 Share on other sites More sharing options...
slappadudle Posted May 5, 2009 Author Share Posted May 5, 2009 Um well I think that the input is irrelevant because the total displays correctly everywhere in the site until it hits this code, which passes the amount on to the hosted payment gateway... Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826534 Share on other sites More sharing options...
kenrbnsn Posted May 5, 2009 Share Posted May 5, 2009 Please show us the output of <?php echo '$total: ' . $total . '<br>'; echo number_format($total, 2, '.', ','); ?> $total is the input to the number_format() function. Ken Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826645 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 echo $total; I assume $total is $1,234.56? It looks fine to me. What do you want it to look like? The same thing except without the $ sign? Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826720 Share on other sites More sharing options...
kenrbnsn Posted May 5, 2009 Share Posted May 5, 2009 When a number is used as an input to number_format() it MUST NOT be already formated -- i.e. no "$" & no "," <?php $num = 12345.67; $str = '12,345.67'; echo '$num: ' . $num . ', formatted $num: ' . number_format($num,2) . "<br>\n"; echo '$str: ' . $str . ', formatted $str: ' . number_format($str,2) . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-826755 Share on other sites More sharing options...
slappadudle Posted May 6, 2009 Author Share Posted May 6, 2009 Hi all, thanks for replying with your helpful suggestions... I have determined the cause of this problem - the gateway processes three values (name, qty, price) in the same field with COMMA SEPARATED VALUES. It is mistaking the thou-sep comma for a VALUE-sep comma! (The quantity is set to 1 if there is no comma) We didn't realise what was happening until we tested an order for over 2000 and it changed the qty to 2, effectively doubling the total to be even more inaccurate!! SO now my question is - How do I REMOVE the thou-sep comma from the total figure?? Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-827318 Share on other sites More sharing options...
Daniel0 Posted May 6, 2009 Share Posted May 6, 2009 Hence the reason why we wanted you to post the input value Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-827330 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Hi all, thanks for replying with your helpful suggestions... I have determined the cause of this problem - the gateway processes three values (name, qty, price) in the same field with COMMA SEPARATED VALUES. It is mistaking the thou-sep comma for a VALUE-sep comma! (The quantity is set to 1 if there is no comma) We didn't realise what was happening until we tested an order for over 2000 and it changed the qty to 2, effectively doubling the total to be even more inaccurate!! SO now my question is - How do I REMOVE the thou-sep comma from the total figure?? By not storing the value with a comma? You can use str_replace. Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-827437 Share on other sites More sharing options...
slappadudle Posted May 7, 2009 Author Share Posted May 7, 2009 Hi all, thanks for replying with your helpful suggestions... I have determined the cause of this problem - the gateway processes three values (name, qty, price) in the same field with COMMA SEPARATED VALUES. It is mistaking the thou-sep comma for a VALUE-sep comma! (The quantity is set to 1 if there is no comma) We didn't realise what was happening until we tested an order for over 2000 and it changed the qty to 2, effectively doubling the total to be even more inaccurate!! SO now my question is - How do I REMOVE the thou-sep comma from the total figure?? By not storing the value with a comma? You can use str_replace. Sounds great, but could you please show me how? I need to use $total = $db->f("order_total"); to extract the figure, what code follows to remove the thou-sep comma then publish the result? Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-828177 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2009 Share Posted May 7, 2009 Again, please show us what is in $total after <?php $total = $db->f("order_total"); ?> do <?php $total = $db->f("order_total"); echo '$total: ' . $total . '<br>'; ?> and post the result. Ken Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-828188 Share on other sites More sharing options...
slappadudle Posted May 7, 2009 Author Share Posted May 7, 2009 Again, please show us what is in $total after <?php $total = $db->f("order_total"); ?> do <?php $total = $db->f("order_total"); echo '$total: ' . $total . '<br>'; ?> and post the result. Ken Here is the result following an order totalling $4521.00: $total: 4521.00000 The payment gateway doesn't like echo $total; for some reason, it says 'Payment amount: 0.00 is invalid.' I then tried echo number_format($total, 2); to remove all the extra zeros but its still adding the thou-sep comma. Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-828201 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 setlocale(LC_MONETARY, 'en_US'); $total = $db->f('order_total'); echo 'Total: ' . money_format('%n', $total) . '<bt />'; That should do it then. Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-828240 Share on other sites More sharing options...
slappadudle Posted May 8, 2009 Author Share Posted May 8, 2009 setlocale(LC_MONETARY, 'en_US'); $total = $db->f('order_total'); echo 'Total: ' . money_format('%n', $total) . '<bt />'; That should do it then. Great, this seems to work. We can now launch the site. Thank you so much for your solution, you have all been incredibly helpful - especially Ken2k7!!! Cheers, RM Quote Link to comment https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/#findComment-829162 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.