php 20%

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

php 20%

Post by YoussefSiblini »

Hi Guys,
I want to take 20% off my product price which is 006385 it has to be like this as HSBC accept them this way, this means that the product price is $63.85.
I tried this:

Code: Select all

	
$TwePercent = 006385 * 0.2;   // get the 20% amount       
$PriceWithTwenPercOff = 006385 - $TwePercent;  // the price after we take the 20% off
the above $TwePercent outout is:1277
$PriceWithTwenPercOff is outputing: 6374.8

I think the $PriceWithTwenPercOff should be 005108 ($51.08).

So over all, how to get the 80% of the 006385, how can I achieve this keeping the format this way like 51.85 will read 005185?


Joe
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php 20%

Post by Celauran »

The leading zeroes are going to cause problems. I recommend storing the prices normally and only formatting them before sending them to HSBC.

Code: Select all

<?php

$price = 63.85;
$discount = 0.20;
$discounted = number_format($price * (1 - $discount), 2);
$discounted = str_pad(str_replace('.', '', $discounted), 6, '0', STR_PAD_LEFT);
echo $discounted;
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: php 20%

Post by YoussefSiblini »

Thank you this did it,
Can you please tell me how these 2 lines works, so I know how to use it for future use, I am still newbie with php :)

$discounted = number_format($price * (1 - $discount), 2);
$discounted = str_pad(str_replace('.', '', $discounted), 6, '0', STR_PAD_LEFT);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php 20%

Post by Celauran »

YoussefSiblini wrote:Thank you this did it,
Can you please tell me how these 2 lines works, so I know how to use it for future use, I am still newbie with php :)

$discounted = number_format($price * (1 - $discount), 2);
$discounted = str_pad(str_replace('.', '', $discounted), 6, '0', STR_PAD_LEFT);

Code: Select all

$discounted = number_format($price * (1 - $discount), 2);
number_format()
Since we're dealing with currency, we know it must be only 2 decimal places. Your example happened to work out nicely, but what if the discount had been 25%? This just rounds to two decimal places.

Code: Select all

$discounted = str_pad(str_replace('.', '', $discounted), 6, '0', STR_PAD_LEFT);
Here we're removing the decimal since HSBC doesn't want it, then we pad the string with 0 on the left until it's 6 characters long.
str_pad()
str_replace()
YoussefSiblini
Forum Contributor
Posts: 206
Joined: Thu Jul 21, 2011 1:51 pm

Re: php 20%

Post by YoussefSiblini »

Thank you sooooo much.
You made it so easy and nicely clear for me.
Post Reply