Gossamer Forum
Home : General : Perl Programming :

Calculate tax for a sale

Quote Reply
Calculate tax for a sale
I have to figure out how to calculate tax on a purchase in a perl script for Florida, USA.

example :

soccer ball $19.95 plus shipping and handling $6.95 plus Florida 6 percent tax.

My math says add 19.95 + 6.95 = 26.90.

add 6 percent to 26.90 = 1.614

totals at : $28.514

Okay how do I round off correctly in perl ? for the customer and the seller to both be happy ?

Is there a legal commerce formula for this that I can use in perl ?

Thanks

Kode -- not an accountant / or math whiz / or Florida merchant

Last edited by:

kode: Jul 24, 2002, 6:43 PM
Quote Reply
Re: [kode] Calculate tax for a sale In reply to
Code:
my $total_to_two_decimal_places = sprintf("%.2f", $price);

You may also want to look at ceil() in the POSIX module.

Last edited by:

Paul: Jul 25, 2002, 2:58 AM
Quote Reply
Re: [kode] Calculate tax for a sale In reply to
Also double check with the local tax office (assessor/collector) because I don't think you tax the shipping & handling... I think it is "price x tax + shipping."

Mike.
Fatboy/Mountainbiker

Last edited by:

Watts: Jul 26, 2002, 9:07 AM
Quote Reply
Re: [Watts] Calculate tax for a sale In reply to
>>
Fatboy/Mountainbiker
<<

Cool
Quote Reply
Re: [kode] Calculate tax for a sale In reply to
Not totally related, but might be a handy snippet of code for you later.

Code:
sub dollar_str_subroutine {
# --------------------------------------------------
# create the dollar string creation function
# takes advantage of closures to speed up the operation
#

my $currency_pre_symbol = '';
my $currency_post_symbol = '$';
my $currency_group_separator = ',';
my $currency_digits_ingroup = 3;
my $currency_decimal_point = '.';
my $currency_decimal_places = '2';

my $dollar_str_sub = sub {
my $dollar_amount = shift || 0;
my $dollar_str = sprintf( "%.0".$currency_decimal_places."f", $dollar_amount );

if ( $currency_group_separator ) {
my $i = index( $dollar_str, '.' ) || '';
my $cents = substr( $dollar_str, $i+1 ) || '';
my $dollars = reverse substr( $dollar_str, 0, $i );
$dollars =~ s,(\d{$currency_digits_ingroup}),$1$currency_group_separator,go;
$dollars =~ s,\Q$currency_group_separator\E$,,o;
$dollar_str = reverse( $dollars ) . $currency_decimal_point . $cents;
}
return $currency_pre_symbol . $dollar_str . $currency_post_symbol;
};

return $dollar_str_sub;
}

# EXAMPLE:
my $dsub = dollar_str_subroutine();
print $dsub->( 25.50 );

It's a little wierd that it creates the sub, it should be easy enough for you to convert that into a normal subroutine. I use it in my shopping cart to display currency amounts. It pulls the basic values from a config file, creates the subroutine once and uses it all over the place... for the sake of efficiency
Quote Reply
Re: [Aki] Calculate tax for a sale In reply to
Thanks for all the answers.

I am awaiting an answer from the firms accountant about how they wish to handle the taxes.

I think it will be as suggested sales tax only on the product and not the S/H charge.

Just the part about how to round correctly for customers and accountants the amount,

still is unknown to me.

So ignoring the S/H for now :

So the soccer ball at $19.95 + 6% [ 1.197 ] = $21.147.

Generally in my Math this would be $21.15 charged to the customer

I am assuming round up if the 3rd decimal is 5 or greater, down if 4 or less.

This works in my mind for 1 item, but what happens when 10 are bought ?

example : 10 balls at $19.95 = $199.50 + 6% [11.97 ] = $211.47 [ no rounding required ]

where as above 10 times the rounded figure of $21.15 = $211.50 = 3 cents more

So it baffles me, as to tax on a single item and rounding and tax on multiple items.

Oh well I will have to wait on the firms accountant and figure the formula from there.

Thanks

Kode
Quote Reply
Re: [kode] Calculate tax for a sale In reply to
The number_ologists [ accountants ] have replied to me.

Sales taxes are on goods only not S/H , add S/H after.

Tax rounds on : .005 up and .004 down.

And to answer my silly 10 purchases:

If a customer buys 10 in one call tax as one purchase X 10 plus tax.

If a customer calls 10 times and buys 1 each time , add tax per each single sale [ 10 different sales ].

I suppose this makes sence as each purchase / call is then treated as one sale at the

cash register.

I was just trying to make sure I did not write a price + tax + S/H , that after 1 billion

sales would be out a penny per sale [ i don't wish to pay the tax man a penny on 1 billion sales ].

Thanks

Kode
Quote Reply
Re: [kode] Calculate tax for a sale In reply to
i don't think you tax s&h. amazon.com doesn't..

subtotal + tax(subtotal) + shipping & handling = total

my $taxpercent = 6.00;
my $shipping = $6.95;

my $subtotal = 19.95;
my $tax = $subtotal * $taxpercent / 100;

my $total = sprintf ("%.2f", ($subtotal + $tax + $shipping));