Currency

授权协议 未知
开发语言
所属分类 jQuery 插件、 其他jQuery插件
软件类型 开源软件
地区 不详
投 递 者 卢志行
操作系统 未知
开源组织
适用人群 未知
 软件概览

Currency: an unobstrusive automatic and real time currency conversion

This is a small tool that may prove to be usefull. It permits automatically converting any currency to any other currency on a website, with real time quotes. It is

implemented using an ajax PHP script backend (for cross-domain issue).

How to install:

Simply copy the currency-ajax.php file to the website (for example in the root folder), the jquery.currency.js and currency.css files. In addition, you can use the set

of icons provided in the package, or your own.

How it works:

Currency reads automatically traverses the DOM and processes every element tag with class currency. Each automatically converted currency value uses the

rel= tag for the options.

Here is an example:

<span class="currency" rel="USD:EUR:&euro;">24.95</span>

The first parameter is the currency "from". The second parameter is the currency "to" and the optional third parameter is the currency symbol to be prepended to the

converted numeric value.

In addition to converting the value, the script also add the "currency to" code to the element class. Using CSS you can style the .currency element to have a country

flag for example based on the currency code.

Ajax

Currency uses ajax and in order to bypass cross-domain restrictions, it uses a simple .php script. The script sends a request to yahoo finance to retrieve the latest

rates for the "from" and "to" currencies.

jQuery cookie

Currency uses the jquery.cookie plugin to store the retrieve rates. Our implementation uses a modified version of the cookie plugin to accept delays in hours instead of

days (see the delay set to 6 in the currency script). The purpose of this is to prevent querying the ajax backend everytime a user reloads the page.

Code

jquery.currency.js

/**
* Currency (http://www.reality-xp.com)
* A jQuery plugin for converting currencies
*
* Version 1.0
* August 27th, 2008
*
* Copyright (c) 2008 Reality XP
* Dual licensed under the MIT and GPL licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-license.php
*
**/

//on page load call convert
$(document).ready(function(){$('.currency').each(function(i,domEle){$(domEle).convertCurrency(false);return true;})});

;(function(){

var $$;

$$ = jQuery.fn.convertCurrency = function(currencycode) {
var $this = $(this)
if ($this.attr('rel'))
{
var prms = $this.attr('rel').split(':'); /*"USD:EUR:€"*/
var fAmnt = parseFloat($this.text());
var cCode = currencycode ? ' '+prms[1] : '';
// check if the exchange rate has been retrieved today
var cookieVal = $.cookie('currencyrate'+prms[0]+prms[1]);
if (cookieVal != null)
{
frmtCurrency($this,prms[2],fAmnt*parseFloat(cookieVal),cCode,prms[1]);
}
else
{
try {
reqAjax = $.ajax({
type: "POST",
url: '/currency-ajax.php',
dataType: "json",
data: "action=rate" + "&currfrom=" + prms[0] + "&currto=" + prms[1],
success: function(json) {
switch (json.errcode) {
case 'ERR-100':
$.cookie('currencyrate'+prms[0]+prms[1],json.result,{expires: 6, path: '/' });
frmtCurrency($this,prms[2],fAmnt*parseFloat(json.result),cCode,prms[1]);
break;
case 'ERR-200':
break;
default:
break
}
},
error: function(xhr, msg, ex) {
reqAjax = null
}
})
} catch(e) {
}
}
}
return this;

function frmtCurrency(ele,symb,val,code,cls) {
// round the currency to the nearest .05
val *= 2.0;
val = val.toFixed(1) / 2.0;
val = val.toFixed(2);
// build the text in the form: '$' '12.35' 'USD'
ele.text(symb+val+code );
// add the currency code to the element class.
ele.addClass(cls);
};

};

})();

currency-ajax.php

<?php
/*
Filename:         rxpcur-ajax.php
Date:             2008-08-27
Copyright:         2008, Reality XP
Author:         Reality XP
Description:     PHP Back-end to fetch Currency Conversion Rates from Yahoo! Finance.
License:        GPL
Inspired by:    http://www.talkphp.com/general/1422-creating-simple-currency-converter-a...
                http://chaos-laboratory.com/2007/03/01/currex-ajax-based-currency-conver...
*/

// Exit if no function specified
if( !isset( $_POST['action'] ) || '' == $_POST['action'] ) {
    echo
'{ errcode: "ERR-000", errmsg: "No action specified" }';
    exit();
}

switch (
$_POST['action']) {
   
    case
'rate':
       
$currfrom = $_POST['currfrom'];
       
$currto = $_POST['currto'];
       
$conversion_rate = get_conversion_rate( $currfrom, $currto );
           
        if(
$conversion_rate != false ) {
           
$result = $conversion_rate * 1.0;
            echo
'{ errcode: "ERR-100", errmsg: "Conversion Successful", result: "' . $result . '" }';
            exit();
        }
        else {
            echo
'{ errcode: "ERR-200", errmsg: "Error contacting Yahoo! Finance" }';
            exit();
        }
        break;
       
    default:
        echo
'{ errcode: "ERR-210", errmsg: "Unknown conversion error" }';
        exit();
}

function
get_conversion_rate( $cur_from, $cur_to ) {

    if(
strlen( $cur_from ) == 0 )
       
$cur_from = "USD";

    if(
strlen( $cur_to ) == 0 )
       
$cur_to = "USD";

    if (
$cur_from == $cur_to)
        return
"1.0";
               
   
$data = "";
   
$host = "download.finance.yahoo.com";
   
$fp = @fsockopen( $host, 80, $errno, $errstr, 30 );
    if ( !
$fp ) {
       
$errorstr = "$errstr ($errno)<br />\n";
        return
false;
    }
    else {
       
// Build Query String
        // Query URL: http://download.finance.yahoo.com/d/quotes.csv?s=[$cur_from][$cur_to]=X&f=l1
        // Returns: A Plain Text file which contains the current exchange rate
        // Example content: 32.15
        //                    The source and destination currencies used here
        //                    were USD (US Dollar) and GBP (Great Britain Pound)
       
$file = "/d/quotes.csv";
       
$str = "?s=" . $cur_from . $cur_to . "=X&f=l1";
       
$out = "GET " . $file . $str . " HTTP/1.0\r\n";
       
$out .= "Host: www.yahoo.com\r\n";
       
$out .= "Connection: Close\r\n\r\n";
       
        @
fputs( $fp, $out );
        while( !@
feof( $fp ) )
           
$data .= @fgets( $fp, 128 );
        @
fclose( $fp );

        @
preg_match( "/^(.*?)\r?\n\r?\n(.*)/s", $data, $match );
       
$data = $match[2];
        return
$data;
    }
//else
}//end get_conversion
   
?>

sample use

with automatic conversion from USD to EUR

<span class="currency" rel="USD:EUR:&euro;">24.95</span>

with automatic conversion from USD to GBP

<span class="currency" rel="USD:GBP:&pound;">24.95</span>

with no conversion to display USD with flag from the CSS (same styling as with other converted currencies

<span class="currency USD">$24.95</span>

Example html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='copyright' content='Copyright (c) 2001-2008 by Reality XP' />
<title>JQuery Currency</title>
<script src="/scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="/scripts/jquery.cookie.js"></script>
<script type="text/javascript" src="/scripts/jquery.currency.js"></script>
<link href="/currency.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<span class="currency" rel="USD:EUR:&euro;">24.95</span>
<span class="currency" rel="USD:GBP:&pound;">24.95</span>
<span class="currency USD">$24.95</span>
</body>
</html>

  • JDK8在线Api中文手册 JDK8在线Api英文手册 Currency 类    Currency类封装了有关货币的信息,没有定义构造函数。表1列出了Currency类支持的方法。下面的程序演示了Currency类: //Demonstrate Currency. import java.util.Currency; import java.util.Locale; class CurDemo

  • import os import requests URL_BASE = "https://www.amdoren.com/api/currency.php" TESTING = os.getenv("CI", False) API_KEY = os.getenv("AMDOREN_API_KEY", "") if not API_KEY

 相关资料
  • 问题内容: 例如,中国货币具有代码。由于限制了使用该货币进行的全球自由交易,因此还有另一种等价的“离岸”货币。维基百科对此有一些总结。 在中,提供了一种方法来更新 JVM附带的三个字母代码集。但是,它不能用于在现有的国家/地区代码中添加单独的货币代码:它将替换为,这对我的目的不利。 如何在不覆盖的情况下添加(不在列表中)可用货币集? 换句话说,如何为一个国家获得多个货币代码? 请注意以下问题:如何

  • 问题内容: 我正在使用UserType 3.0.0.RC1将JodaMoney映射到Hibernate。 当SessionFactory初始化时,我陷入了一个错误: PersistentMoneyAmount要求将currencyCode定义为参数,或者将defaultCurrencyCode Hibernate属性定义为 我确定我必须遇到一些配置问题-这是相关的代码段。 Persistence.

  • 问题内容: 我是Swift编程的新手,我一直在Xcode 8.2中创建一个简单的小费计算器应用程序,在下面的代码中进行了设置。但是,当我实际运行我的应用并输入要计算的金额(例如23.45)时,它会显示超过2个小数位。在这种情况下,如何将其格式化为? 问题答案: 如果要将货币强制为$,可以使用此字符串初始化程序: 如果希望它完全取决于设备的语言环境设置,则应使用。这将考虑到货币的小数位数以及正确放置

  • 我有一个输入字段,它使用JavaScript将数字enteret转换成丹麦货币(20000将显示为20.000,00KR)..在此输入中,我需要验证(使用passley validate)以下内容: 输入的只有数字。 数字介于10000和200000之间。 我尝试使用data-parsley-type=“digits”data-parsley-length=“[5,6]”,该offcause验证该

  • 谢谢你的预付费。

  • react-intl-currency-input A React component for i18n currency input using Intl API. Installation $ npm install react-intl-currency-input --save-dev How to use import React from "react"import IntlCurre

  • ngx-currency Demo https://nbfontana.github.io/ngx-currency/ Table of contents About Installation Documentation Development License About Getting Started Installing and Importing Install the package by

  • Free Currency Rates API In the name of God, who have guided me to do this work Features: Free & Blazing Fast response No Rate limits 150+ Currencies, Including Common Cryptocurrencies Daily Updated UR

相关阅读

相关文章

相关问答

相关文档