0

Hi I need to remove 10% from a shopping carts subtotal

Original code:

<?php echo number_format($order->subtotal,2);?>&OID=<?php echo $order->trans_id;?>

I know it's not precise, but would something like this work?

<?php echo number_format($order->subtotal * 0.909090909,2);?>&OID=<?php echo $order->trans_id;?>

Thanks

GibsonFX
  • 1,000
  • 2
  • 10
  • 33
  • Almost, but just multiply by 0.9, that takes 10% off (10 * 0.9 = 9, 2000 * 0.9 = 1800 etc) – dops Apr 12 '14 at 09:25
  • I need to take off 10% before it's added. ie if you add 10% to 100 it becomes 110, but if you take 10% from 110 it becomes 99. I need it to work back to 100. If that makes sense, I need it to be 10% before it's added. :) – GibsonFX Apr 12 '14 at 16:46

3 Answers3

1

Use sprintf()

$a =  2324.56*0.909090909 ;

echo sprintf('%0.2f',$a);

output // 2113.24

sprintf() will handle the floating point precession which is the best way to handle.

If needs to display the money format for specific locale it could be doing using money_format

$a =  2324.56*0.909090909 ;

$amount =  sprintf('%0.2f',$a);

setlocale(LC_MONETARY, 'en_US');

echo money_format('%(#1n', $amount) . "\n";
output // $2,113.24

Here is an explanation on number_format() -ve value precession issue

http://www.howtoforge.com/php_number_format_and_a_problem_with_negative_values_rounded_to_zero

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • meh, number format is more appropriate in this case, im sure he's looking for 2,113.24, not 2113.24 – Bryan Apr 12 '14 at 08:49
  • I guess. but you could save 3 function calls by using `number_format()` instead. What's better about `sprintf()`? – Bryan Apr 12 '14 at 09:07
  • check here.. issues of number_format() ..its very poor on decimal precision and the – Abhik Chakraborty Apr 12 '14 at 09:12
  • bad link, I am curious though – Bryan Apr 12 '14 at 09:13
  • I have added the link in the answer !! – Abhik Chakraborty Apr 12 '14 at 09:16
  • check more http://stackoverflow.com/questions/9194772/php-number-format-decimal-99-is-turned-to-00 http://stackoverflow.com/questions/6936062/why-is-my-number-value-changing-using-number-format – Abhik Chakraborty Apr 12 '14 at 09:19
  • Thanks for those links, very interesting. – Bryan Apr 12 '14 at 09:19
  • well if u make it short then yes number_format() does the trick. But to be honest should use money_format() or PECL http://www.php.net/manual/en/class.numberformatter.php to handle currency. – Abhik Chakraborty Apr 12 '14 at 09:23
  • i don't like money_format, it displays the currency like `USD` instead of the symbol. None the less. solid information. Thanks for sharing. – Bryan Apr 12 '14 at 09:25
  • well if u execute my above code in the answer it will echo $ instead of USD :-) – Abhik Chakraborty Apr 12 '14 at 09:27
  • true. I Don't really feel like arguing over it. Your information is good, and +1 for sharing all im saying is, with positive values less than the limit for floating point numbers `'$'.number_format()` works equally well in one line instead of 5. – Bryan Apr 12 '14 at 09:30
  • Thank you for your input, but I need it to work in that particular piece of code as it's for an affiliate % calculation and it's the piece of code the affiliate company has given me, so I don't want to mess with it too much. Plus it's in AUD, not USD. Thanks – GibsonFX Apr 12 '14 at 16:52
0

try this

$subtotal = $order->subtotal;
$cut_subtotal = $subtotal *(10/100);
$subtotal_new = $subtotal-$cut_subtotal;

Now use this in your code

<?php echo number_format($subtotal_new,2);?>

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • forgot the underscore in the last part for cut_subtotal. – Bryan Apr 12 '14 at 08:48
  • I need to remove 10% of the pre tax figure, taking a direct 10% off would be too much. I'm working with Cart66 and they don't have a way to display prices including tax to the customers before check out. That's not how we display prices in Australia, so we've had to include the tax in the prices. So it need to remove the 10% tax that was added. i.e. add 10% to 100 it becomes 110, take 10% from 110 it becomes 99 I would need to make 110 back to 100. If that makes sense? – GibsonFX Apr 12 '14 at 16:57
0

that probably would work, but why not just subtract the 10 percent? If that's the goal, why not just do it? keep in mind number_format rounds up, but I expect that's desired.

<?php echo number_format( ($order->subtotal - ($order->subtotal* .1) ) ,2);?>

This is the math that actually subtracts 10% why not use this instead of something that's close?

Bryan
  • 3,449
  • 1
  • 19
  • 23
  • Would that be 10% after or before the fact? It's a tax deduction so, it needs to remove 10% before it was added to the subtotal, if you take 10% from the subtotal, it's too much. Cheers – GibsonFX Apr 12 '14 at 15:05
  • this is subtotal minus 10 percent of subtotal, your question is a bit confusing, but hope that answers it. – Bryan Apr 12 '14 at 17:24
  • ahh after reading the comment you added to you OP I understand why your asking about this. Why can't you just save the tax amount and subtract it? Or get the original amount before tax? – Bryan Apr 12 '14 at 17:42
  • Because if you use the carts built in tax system it doesn't display the prices in the shop including tax, and that makes things really difficult. – GibsonFX Apr 14 '14 at 07:05