我需要稍微调整一下我们的脚本。如果将INR货币低于1.2并且不进行四舍五入,那将会是什么修改,如下所示,其他货币(除了英镑)之外不会发生舍入?
<?php
require_once "tools.php";
function GetRate($curCode)
{
if ($curCode=="GBP")
return 1;
if ($curCode=="USD")
return 1.5399;
if ($curCode=="AUD")
return 1.9838;
if ($curCode=="CAD")
return 1.9168;
if ($curCode=="INR")
return 1;
/* $url = "http://rate-exchange.appspot.com/currency?from=GBP&to=".$curCode;
$s = ReadPageCURL($url);
if ($ar = GetStringBetweenTags('rate":',',',$s))
return floatval(trim($ar[0]));*/
else
return 1;
}
function Convert($s,$curCode)
{
$path = "/home2/busi6292/public_html/orders/convertcurrency/automatic/";
if ($curCode=="GBP")
{
$resFile=$path."bigcommerce_uk.xml";
$addURL="?setCurrencyId=1";
}
else
if ($curCode=="USD")
{
$resFile=$path."bigcommerce_usa.xml";
$addURL="?setCurrencyId=3";
}
else
if ($curCode=="INR")
{
$resFile=$path."bigcommerce_uk_revised2.xml";
$addURL="?setCurrencyId=1";
}
else
if ($curCode=="CAD")
{
$addURL="?setCurrencyId=6";
$resFile=$path."bigcommerce_canada.xml";
}
else
if ($curCode=="AUD")
{
$addURL="?setCurrencyId=4";
$resFile=$path."bigcommerce_australia.xml";
}
$curRate = GetRate($curCode);
echo ("<br>$curCode<br>$curRate<br>$resFile<Br><br>");
$eTag=' GBP';
$eTagLen=strlen($eTag);
$res=$s;
if ($curCode!="GBP")
{
$res=str_replace(".html]]></link>",".html".$addURL."]]></link>",$res);
$res=str_replace("/]]></link>","/".$addURL."]]></link>",$res);
while (($ep=strpos($res,$eTag,0))!==false)
{
if (@file_exists("stop.txt")) { @unlink("stop.txt"); die ("<br>stopped"); }
$sp=$ep-1;
while ($sp>=0)
{
if (substr($res,$sp,1)=="[")
{
$sp++;
$lastPos=$ep+$eTagLen;
$s1=substr($res,$sp,$lastPos-$sp);
//pre-divide the s1 force to float and apply pre-rounding
$preDivide = round((floatval($s1)/1.2), 2);
//old line before pre-divison and rounding change
//$newPrice=sprintf("%1.4f",(floatval($s1)/1.2*floatval($curRate)))." ".$curCode;
$newPrice=sprintf("%1.4f",($preDivide*floatval($curRate)))." ".$curCode;
//double checks the round and forces only 2 decimal places
$roundPrice = number_format((float)$newPrice, 2, '.', '');
//old line before pre-division and rounding change
// $res=str_replace("[".$s1."]","[".$newPrice."]",$res);
$res=str_replace("[".$s1."]","[".$roundPrice."]",$res);
file_put_contents($resFile,$res);
$FileSize2 = filesize($resFile);
//WriteToLog("log_".$curCode.".txt","1. sp=$sp, ep=$ep, lastPos=$lastPos, old price = $s1, newPrice = $newPrice. x1=".sprintf("%1.4f",floatval($s1)/1.2)." x0=".sprintf("%1.4f",$s1));
//die ("s1=$s1, newPrice=$newPrice");
break;
}
$sp--;
}
//WriteToLog("log_".$curCode.".txt","2. sp=$sp, ep=$ep, lastPos=$lastPos, old price = $s1, newPrice = $newPrice. x1=".sprintf("%1.4f",floatval($s1)/1.2)." x0=".sprintf("%1.4f",$s1));
}
file_put_contents($resFile,$res);
$FileSize1 = filesize($resFile);
if($FileSize1 == '0' || $FileSize2 == '0')
{
$t=@microtime(true);
$url = "http://www.MYDOMAIN.co.uk/xml.php?GSF=88d6badb/GB";
$s = ReadPageCURL($url);
file_put_contents("original.xml",$s);
Convert($s,"GBP");
Convert($s,"USD");
Convert($s,"CAD");
Convert($s,"AUD");
Convert($s,"INR");
$t=@microtime(true)-$t;
echo "<br>Cron finished, elapsed time = ".sprintf("%1.3f",$t);
}
// if ($curCode!="GBP")die ("<br>abort");
} // replace prices
}
$t=@microtime(true);
$url = "http://www.MYDOMAIN.co.uk/xml.php?GSF=88d6badb/GB";
$s = ReadPageCURL($url);
file_put_contents("original.xml",$s);
Convert($s,"GBP");
Convert($s,"USD");
Convert($s,"CAD");
Convert($s,"AUD");
Convert($s,"INR");
$t=@microtime(true)-$t;
echo "<br>Cron finished, elapsed time = ".sprintf("%1.3f",$t);
?>
答案 0 :(得分:0)
在执行该行之前,只需检查INR。
if($curCode != "INR")
{
$preDivide = round((floatval($s1)/1.2), 2);
}
else
{
$preDivide = floatval($s1);//or whatever treatment you want to give to INR
}