Gossamer Forum
Home : General : Perl Programming :

Correcting Decimal In Currency

Quote Reply
Correcting Decimal In Currency
Hi All.

I have the following part of a script which works out the TAX/VAT fro order.

Code:
######## including vat/tax to total amount #######
my $tax_per = getTax();
my ($total_amount_old,$total_amount_tax);
$total_amount_old = $grand_total;
if($tax_per ne "")
{
$total_amount_tax = $grand_total * (getTax()/100);
my $pt = index($total_amount_tax,".");
if($pt eq "-1")
{
$total_amount_tax = $total_amount_tax . ".00";
}
$total_amount = $grand_total + $total_amount_tax;
##print "total_amount_tax = " . $total_amount_tax;
}
######## end of including vat/tax to total amount #######

######## formatting total amount #######
my $pt = index($total_amount,".");
if($pt ne "-1")
{
my $before_decimal = substr($total_amount,0,$pt);
my $after_decimal = substr($total_amount,$pt+1);
$after_decimal = substr($after_decimal,0,2);
$total_amount = $before_decimal . "." . $after_decimal;
##print "before_decimal = " . $before_decimal . "<br>";
##print "after_decimal = " . $after_decimal . "<br>";
}
else
{
$total_amount = $total_amount . ".00";
}
######## end of formatting total amount #######


The problem im having is that the TAX / VAT ammount has too many digits after the decimal the other figures are ok. Heres an example?

Total Amount: GBP 24.99
Vat/Tax: GBP 4.37325

Grand Total: GBP 29.36

How can I correct this?

Thanks in advance!
Quote Reply
Re: [mdj1] Correcting Decimal In Currency In reply to
I would recommend looking into sprintf() and printf() ... these can be used to limit the number of decimal places a number has.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Correcting Decimal In Currency In reply to
Thanks for that. I have it working in the following line and it displays fine.

Code:
print '<br><font face=$normal_font_style size=$normal_font_size><b>Vat/Tax: '. $queryresult_config[1] .''. sprintf("%.2f", $total_amount_tax) .' </b></font>';


But im having problems getting it to display in the following script as its not within a print command.

Code:
$ord_report = $ord_report . <<END_OF_TEXT;
<td colspan=4 align=right>
<font face=$normal_font_style size=$normal_font_size><b>Total Amount: $queryresult_conf[1] $total_amount_old</b></font>
<br><font face=$normal_font_style size=$normal_font_size><b>Vat/Tax: $queryresult_config[1] sprintf("%.2f", $total_amount_tax) </b></font>
<br><font face=$normal_font_style size=$normal_font_size><b>Grand Total: $queryresult_conf[1] $total_amount</b></font>
</td>
</tr>
</table>
END_OF_TEXT


Could someone let me know if there is a solution to this??
Regards

Mark
http://www.host4.me.uk

No doubt i'll be back for more answers!
Quote Reply
Re: [mdj1] Correcting Decimal In Currency In reply to
Glad it worked for you. I would recommend trying something like;

Code:
$ord_report .= qq|
<td colspan=4 align=right>
<font face=$normal_font_style size=$normal_font_size><b>Total Amount: $queryresult_conf[1] $total_amount_old</b></font>
<br><font face=$normal_font_style size=$normal_font_size><b>Vat/Tax: $queryresult_config[1] |;

$ord_report .= sprintf("%.2f", $total_amount_tax);

$ord_report .= qq|</b></font>
<br><font face=$normal_font_style size=$normal_font_size><b>Grand Total: $queryresult_conf[1] $total_amount</b></font>
</td>
</tr>
</table>
|;

Not tested... but it should work. Basically, you can't call sprintf() inside the statement, because it will only parse basic variables in there, but not do formatting like that.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Correcting Decimal In Currency In reply to
Hi Andy,

Thanks once again, but now I have found another problem, I think it lies in the following piece of code.

Code:
######## including vat/tax to total amount #######
my $tax_per = getTax();
my ($total_amount_old,$total_amount_tax);
$total_amount_old = $total_amount;
if($tax_per ne "")
{
$total_amount_tax = $total_amount * (getTax()/100);
my $pt = index($total_amount_tax,".");
if($pt eq "-1")
{
$total_amount_tax = $total_amount_tax . ".00";
}
$total_amount = $total_amount + $total_amount_tax;
##print "total_amount_tax = " . $total_amount_tax;
}
######## end of including vat/tax to total amount #######


This is the problem.

Total Amount: GBP 11.99
Vat/Tax: GBP 2.10

Grand Total: GBP 14.08

The VAT rate is 17.5% if i do the vat of 17.5% on 11.99 it works out to 2.098 which is correct at 2.10 as shown as its rounded up. But in the code above it seems to be adding the 2.09 to get the grand total.


Regards

Mark
http://www.host4.me.uk

No doubt i'll be back for more answers!
Quote Reply
Re: [mdj1] Correcting Decimal In Currency In reply to
How about this?

Code:
my $tax_per = getTax();
my ($total_amount_old,$total_amount_tax);
$total_amount_old = $total_amount;
if($tax_per ne "")
{
$total_amount_tax = $total_amount * (getTax()/100);
my $pt = index($total_amount_tax,".");
if($pt eq "-1")
{
$total_amount_tax = $total_amount_tax . ".00";
}
$total_amount = sprintf("%.2f", $total_amount) + sprintf("%.2f", $total_amount_tax);
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!