Login   Register  
PHP Classes
elePHPant
Icontem

File: class_currency_convert.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Stephane Mouton  >  Currency Display & Convert  >  class_currency_convert.php  >  Download  
File: class_currency_convert.php
Role: ???
Content type: text/plain
Description: Class to convert from a currency to an other
Class: Currency Display & Convert
Currency conversion and display with locale format
Author: By
Last change:
Date: 13 years ago
Size: 2,916 bytes
 

Contents

Class file image Download
<?
if(!defined("_CLASS_CURRENCY_CONVERT_LOADED")) {
	define("_CLASS_CURRENCY_DISPLAY_LOADED", true);

	// ============================================================
	// ================ CURRENCY CONVERT ==========================
	// ============================================================
	// == version : 1.1	    (class_currency_convert.php)
	// ============================================================
	// ==== Description
	// == Currency conversion class : convert from one currency to another one
	// ==== Relationships :
	// == None, but may be ideally used with CurrencyDisplay class
	// ============================================================
	// ==== History :
	// == 16/11/2000 : S. Mouton	First Version
	// == 29/11/2000 : S. Mouton	Added euro conversion euro
	// == 27/02/2001 : S. Mouton	Full re organisation : separate between DB and non DB version
	// == 14/03/2001 : S. Mouton	First functional version in new organisation (1.1)
	// ============================================================

	class CurrencyConvert {
		var $idFrom		= ''; // string ID related with the currency to be converted
		var $idTo		= ''; // string ID related with the target currency
		var $rate		= 0.0;	// Conversion rate
		var $rounding	= 1.0;	// Rounding
		var $roundType	= 1;	// Kinf of rounding : 0=Inferior, 1=Normal, 2=Superior, 3=None
		var $nbDecimal	= 2;	// Number of decimals
	// ================		 
	function CurrencyConvert($rate,$rounding,$roundType,$nbDecimal,$idFrom='', $idTo=''){
		$this->idFrom		= $idFrom;
		$this->idTo			= $idTo;
		$this->rate     	= $rate;
		$this->rounding		= $rounding;
		$this->roundType	= $roundType;
		$this->nbDecimal	= $nbDecimal;
	}

	// ================
	function From(){
		return($this->idFrom);
	}
	function To(){
		return($this->idTo);
	}
	
	// ================
	function getValue($nb){
		// 1) Apply conversion rate
		$res = $nb/$this->rate;
		// 2a) Rounding : divide to significant digits
		$res = $res/$this->rounding; 
		// 2b) Rounding : kind of rounding
		switch ($this->roundType){
			// Inferior rounding
			case 0:
				$res=floor($res);
			break;
			// Normal rounding
			case 1:
				$res=round($res);
			// Superior rounding
			case 2:
				$res=ceil($res);
			break;
			// None
			default :
			break;
		}
		// 2c) Rounding : multiply to significant digits
		$res = $res*$this->rounding;
		// 3) Fin de conversion
		return($res);
	}

	function getStringValue($nb){
		$res = "";
		// 1) Number of decimals
		if ($this->nbDecimal != 0){
			$formatString="%.".$this->nbDecimal."f";
			$res=sprintf($formatString,$this->getValue($nb));
		}
		else{
			$res = abs($this->getValue($nb));
		}
		return($res);
	}
	// ================ /CURRENCY CONVERSION ======================
	// ============================================================
	} // end class
}
?>