/******************************************************************* * * Euros - take a price in pounds and print as Euros * * Input: spounds - a string in pounds, URL encoded * * Output: A string formatted in the new currency * ********************************************************************/ function Euros(spounds) { document.write(ConvertCurrency(spounds, eurorate, "€", ",")); // Format & output in Euros } /******************************************************************* * * Dollars - take a price in pounds and print as Dollars * * Input: spounds - a string in pounds, URL encoded * * Output: A string formatted in the new currency * ********************************************************************/ function Dollars(spounds) { document.write(ConvertCurrency(spounds, dollarrate, "$", ".")); // Format & output in Dollars } /******************************************************************* * * ConvertCurrency - take a price in pounds and convert to Currency * * This assumes that the currency symbol goes at the front and * that it has two decimal places. This works for Dollars, Euros etc. * * Input: spounds - a string in pounds, URL encoded * nConversionRate - number of x in a pound * sCurrencySymbol - symbol to display * sDecimalPoint - decimal point symbol * * Returns: A string formatted in the new currency * ********************************************************************/ function ConvertCurrency(spounds, nConversionRate, sSymbol, sDecimalPoint) { var nPounds, nCurrency, nWholeCurrency, nFraction, sFraction; nPounds = ExtractPounds(spounds); // Extract the number of pounds as a float nCurrency = Math.round(nPounds * nConversionRate * 100) / 100; // Work out the new currency nWholeCurrency = Math.floor(nCurrency); // Number of whole Currency nFraction = Math.floor((nCurrency - nWholeCurrency) * 100); // Number of Fraction (2 decimal places) if (nFraction < 10) // Needs to be two digits { sFraction = "0" + nFraction.toString(); // Pad with leading zero } else { // Already has two digits sFraction = nFraction.toString(); // So just format it } return(sSymbol + nWholeCurrency.toString() + sDecimalPoint + sFraction); // Format the number with the symbol } /******************************************************************* * * ExtractPounds - extract a numeric value from an HTML formatted string * * Input: spounds - a string in pounds, URL encoded * * Returns: Nuneric value of pounds & pence * ********************************************************************/ function ExtractPounds(spounds) { var nPoundPos, nDecimalPos, nPoundEnd, nDecimalEnd; var sWholePounds, sPence; // // The formatted value should start with a pound sign // nPoundPos = spounds.indexOf("£"); // Find the pound sign if (nPoundPos < 0) // Bad format { nPoundPos = spounds.indexOf("£"); // Look for URL encoded version if (nPoundPos < 0) // Still not found { return(0); } nPoundEnd = nPoundPos + 6; // Skip URL encoded value } else { nPoundEnd = nPoundPos + 1; // Just skip single character } // // Then look for the decimal point // nDecimalPos = spounds.indexOf("."); // Find the decimal point if (nDecimalPos < 0) // No decimal point { nDecimalPos = spounds.indexOf("."); // Look for URL encoded version if (nDecimalPos < 0) { return(0); } nDecimalEnd = nDecimalPos + 5; // Skip past the match } else { nDecimalEnd = nDecimalPos + 1; } sWholePounds = spounds.substring(nPoundEnd, nDecimalPos); // Get the whole pounds sPence = spounds.substring(nDecimalEnd, spounds.length); // Get the number of pence // // Purists would do more validation here // return(parseInt(sWholePounds) + (parseInt(sPence)/ 100)); // Build a floating point value }