Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/156887-solved-number_format-issue-i-think/
Share on other sites

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

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

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??

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.

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

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.

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  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.