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

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
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

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

Post 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
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

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

Post 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:
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post by Apollo »

Code: Select all

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