Page 1 of 1

HOw to trim(round-off) a var value upto 2 decimal points

Posted: Fri May 07, 2010 5:05 am
by amitdubey2
Hello frns,

I want to round-off the value of a variable upto 2 decimal points,as i am converting indian rupees to dollar....
It is giving dollar value in long decimal digits which cannt be used in paypal or something like that..

Here is the code which i am using to convert the money from INR to Dollar..

Code: Select all

 $amount=get_order_total(); // Amount to convert  

 $from   = 'INR'; //US Dollar - currency to be converted  

 $to     = 'USD'; //to INDIAN RUPEES - result currency  

 $url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';  

 $handle = @fopen($url, 'r');  

 if ($handle) {  

    $result = fgets($handle, 4096);  

    fclose($handle);  

 }  

 $array = explode(',',$result);  

 $RS_value=$array[1];  

 $rs_amount=$amount*$RS_value;  

 //echo $rs_amount ;  

 ?> 
	

If you have any idea how to round off this value ..please post it.



regards
Amit

Re: HOw to trim(round-off) a var value upto 2 decimal points

Posted: Fri May 07, 2010 5:17 am
by amitdubey2
hello...


here is the answer...
it can be done by using math function :arrow:
$var1= round($var,2); //It will round off the value upto 2 decimal places.
echo $var1; // print the rounded off value.


regards
amit :drunk:

Re: HOw to trim(round-off) a var value upto 2 decimal points

Posted: Fri May 07, 2010 5:25 am
by Apollo

Code: Select all

echo sprintf("%.2f",$rs_amount);