SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing math rules in javascript

    Hi all

    Can anyone help me as I have got stuck (again)
    Fotiman (very kindly) coded this script which turns numbers (22) into words (strings (twenty two)
    Then converts this to English and French,
    twenty two dollars 00 cents / vingt deux euros 00 centimes
    Which all works fine.
    but I have 4 main languages on my site English / French / Spanish / German
    so now I need the same script to do the same with Spanish & German but the rules are different,

    German:

    00 > 20 same as English
    20 > 99 repeated the same as English 0 to 9 for each 20,30,40,50,60,70,80,90.
    With a little twist all the numbers are reversed (ones then tens)
    for example: 21 (twenty one) = 1&20 (one & twenty)

    100s 100 = hundred, 101 = hundred one , 102 = hundred two

    200 = two hundred, 201= two hundred one, 202= two hundred two
    Repeated to 900

    1000s the same as hundreds

    So it not that different to the English counting system, but I have tried to change it around without any results
    So can someone point me to the right bit of code I should be looking at. And as the spanish number system is somewhat chaotic I need to know what dose what,
    Fotiman has kindly placed comments for me, and I can change the all the languages in the
    ,'~1': {en: 'one', fr: 'un'} part of the code,

    Hope Ive explained it well enough for you to explan if not let me know,
    Many thanks

    Steve

    Here is the code
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset:utf-8">
      <title></title>
    </head>
    <body>
    
    <script type="text/javascript">
    var i18n = function(){
        return {
            /**
             * Return an internationalized text string.  If no language is specified
             * then it will default to English (en).  If a translated string is not
             * found for the language, then the value passed in (the key) will be
             * returned.
             * @param {String} k The key representing the value to be returned.
             * @param {String} l The language identifier of the string to be returned.
             */
            _ : function(k, l) {
                if (typeof(l) == 'undefined' || l == null || l == '') {
                    l = 'en'; // Default to english
                }
                if (typeof(this[k]) != 'undefined' && 
                    typeof(this[k][l]) != 'undefined') {
                    return this[k][l];
                }
                return k;
            },
            /**
             * Return the internationalized singular or plural version of a string
             * @param {String} s The key for the singular version of the string
             * @param {String} p The key for the plural version of the string
             * @param {Number} n The number to use for determining sigularity
             * @param {String} l The language identifier of the string to be returned
             */
            pluralize : function(s, p, n, l) {
                if (n != 1) {
                    return this._(p, l);
                }
                return this._(s, l);
            }
            /* Below are the strings to be translated. Keep alphabetical by key. */
            ,'~0':                  {en: 'zero', fr: 'zro'}
            ,'~1':                  {en: 'one', fr: 'un'}
            ,'~2':                  {en: 'two', fr: 'deux'}
            ,'~3':                  {en: 'three', fr: 'trois'}
            ,'~4':                  {en: 'four', fr: 'quatre'}
            ,'~5':                  {en: 'five', fr: 'cinq'}
            ,'~6':                  {en: 'six', fr: 'six'}
            ,'~7':                  {en: 'seven', fr: 'sept'}
            ,'~8':                  {en: 'eight', fr: 'huit'}
            ,'~9':                  {en: 'nine', fr: 'neuf'}
            ,'~10':                 {en: 'ten', fr: 'dix'}
            ,'~11':                 {en: 'eleven', fr: 'onze'}
            ,'~12':                 {en: 'twelve', fr: 'douze'}
            ,'~13':                 {en: 'thirteen', fr: 'treize'}
            ,'~14':                 {en: 'fourteen', fr: 'quatorze'}
            ,'~15':                 {en: 'fifteen', fr: 'quinze'}
            ,'~16':                 {en: 'sixteen', fr: 'seize'}
            ,'~17':                 {en: 'seventeen', fr: 'dix-sept'}
            ,'~18':                 {en: 'eighteen', fr: 'dix-huit'}
            ,'~19':                 {en: 'nineteen', fr: 'dix-neuf'}
            ,'~20':                 {en: 'twenty', fr: 'vingt'}
            ,'~30':                 {en: 'thirty', fr: 'trente'}
            ,'~40':                 {en: 'forty', fr: 'quarante'}
            ,'~50':                 {en: 'fifty', fr: 'cinquante'}
            ,'~60':                 {en: 'sixty', fr: 'soixante'}
            ,'~70':                 {en: 'seventy', fr: 'soixante-dix'}
            ,'~71':                 {en: 'seventy one', fr: 'soixante et onze'}
            ,'~72':                 {en: 'seventy two', fr: 'soixante-douze'}
            ,'~80':                 {en: 'eighty', fr: 'quatre-vingts'}
            ,'~90':                 {en: 'ninety', fr: 'quatre-vingt-dix'}
            ,'and' :                {en: 'and', fr: 'et'}
            ,'currencyName' :       {en: 'Dollar', fr: 'Franc'}
            ,'currencyNamePlural' : {en: 'Dollars', fr: 'Francs'}
            ,'fractionalCurrency' : {en: 'cent', fr: 'centime'}
            ,'fractionalCurrencyPlural' : {en: 'cents', fr: 'centimes'}
            ,'hundred' :            {en: 'hundred', fr: 'cent'}
            ,'hundreds' :           {en: 'hundreds', fr: 'cents'}
            ,'invalid amount' :     {en: 'Invalid amount'}
            ,'million' :            {en: 'million', fr: 'million'}
            ,'millions' :           {en: 'millions', fr: 'millions'}
            ,'ten' :                {en: 'ten', fr: 'dix'}
            ,'thousand' :           {en: 'thousand', fr: 'mille'}
            ,'thousands' :           {en: 'thousands', fr: 'mille'}
            ,'twenty' :             {en: 'twenty', fr: 'vingt'}
            ,'twentyone' :          {en: 'twenty one', fr: 'vingt et un'}
        };
    }();
    
    var enMoneyConverter = function() {
        /**
         * Convert a decimal value to textual dollar and cents
         * @param {Float} amt The decimal value
         * @return The textual dollar and cents value
         */
        function toTextualValue(amt) {
            if (amt == 0) {
                return ' ' + i18n._('~0', 'en');
            }            
    
            var result = "";
            var millions = Math.floor(amt / 1000000);
            var m = millions * 1000000;
            var thousands = Math.floor((amt - m) / 1000);
            var th = thousands * 1000;
            var hundreds = Math.floor((amt - (m + th)) / 100);
            var h = hundreds * 100;
            var tens = Math.floor((amt - (m + th + h)) / 10);
            var t = tens * 10;
            var ones = Math.floor((amt - (m + th + h + t)));
    
            if (millions >= 1) {
                result += toTextualValue(millions) + ' ' + i18n._('million', 'en');
            }
            if (thousands >= 1) {
                result += toTextualValue(thousands) + ' ' + i18n._('thousand', 'en');
            }
            if (hundreds >= 1) {
                result += toTextualValue(hundreds) + ' ' + i18n._('hundred', 'en');
            }
            if (tens > 0) {
                if (tens == 1) { // 10 - 19
                    result += ' ' + i18n._('~' + tens + ones, 'en');
                }
                else { // 20 - 99
                    result += ' ' + i18n._('~' + tens + '0', 'en');
                } 
            }
            if (ones > 0 && tens != 1) {
                result += ' ' + i18n._('~' + ones, 'en');
            }
             
            return result;
        }
        
        return {
            toTextDollars : function(amt) {
                if (amt > 999999999.99 || amt < 0) {
                    return i18n._('invalid amount', 'en');
                }
                var dollars = parseInt(amt);
                var cents = parseInt(Math.round((amt - Math.floor(amt)) * 100));
                return (toTextualValue(dollars) + ' ' + 
                        i18n.pluralize('currencyName', 'currencyNamePlural', dollars, 'en') + 
                        ' ' + i18n._('and', 'en') + toTextualValue(cents) + ' ' + 
                        i18n.pluralize('fractionalCurrency', 'fractionalCurrencyPlural', cents, 'en'));
            }
        };
    }();
    
    var frMoneyConverter = function() {
        /**
         * Convert a decimal value to textual francs and centimes
         * @param {Float} amt The decimal value
         * @return The textual francs and centimes value
         */
        function toTextualValue(amt) {
            if (amt == 0) {
                return ' ' + i18n._('~0', 'fr');
            }            
    
            // Split the amount into the multiples of 10 that we would annotate:
            // tens ("twenty x", "thirty x", etc.)
            // hundreds ("one hundred x", "two hundred x", etc.)
            // thousands ("one thousand x", "two thousand x", etc.)
            // millions ("one million x", "two million x", etc.)
            // If we were to go higher, next would be billions and trillions.
            var result = "";
            // Suppose amt = 7,654,321...  
            var millions = Math.floor(amt / 1000000);
            // millions = Math.floor(7654321 / 1000000) 
            //          = Math.floor(7.654321) 
            //          = 7
            var m = millions * 1000000;
            // m = 7 * 1000000 = 7000000 
            var thousands = Math.floor((amt - m) / 1000);
            // thousands = Math.floor((7654321 - 7000000) / 1000)
            //           = Math.floor(654321 / 1000)
            //           = Math.floor(654.321)
            //           = 654
            var th = thousands * 1000;
            // th = 654 * 1000 = 654000
            var hundreds = Math.floor((amt - (m + th)) / 100);
            // hundreds = Math.floor((7654321 - (7000000 + 654000)) / 100)
            //          = Math.floor((7654321 - 7654000) / 100)
            //          = Math.floor(321 / 100)
            //          = Math.floor(3.21)
            //          = 3
            var h = hundreds * 100;
            // h = 3 * 100 = 300
            var tens = Math.floor((amt - (m + th + h)) / 10);
            // tens = Math.floor((7654321 - (7000000 + 654000 + 300)) / 10)
            //      = Math.floor((7654321 - 7654300) / 10)
            //      = Math.floor(21 / 10)
            //      = Math.floor(2.1)
            //      = 2
            var t = tens * 10;
            // t = 2 * 10 = 20
            var ones = Math.floor((amt - (m + th + h + t)));
            // ones = Math.floor((7654321 - (7000000 + 654000 + 300 + 20)))
            //      = Math.floor(7654321 - 7654320)
            //      = Math.floor(1)
            //      = 1
    
            if (millions >= 1) { // if (7 >= 1)
                // 1 million is a special case which our pluralize method can distinguish
                // un million vs. deux millions (trois millions, etc.)
                result += toTextualValue(millions) + ' ' + i18n.pluralize('million', 'millions', millions, 'fr');
            }
            if (thousands >= 1) { // if (654 >= 1)
                // 1 thousand is a special case.  Instead of un mille, it's just mille
                // So we don't call toTextualValue for the 1 in that case
                if (thousands == 1) {
                    // For consistency, I'm calling the pluralize method here. However,
                    // if I wanted to make it slightly more efficient, I could do this
                    // instead (since I know I want the singular version):
                    // result += ' ' + i18n._('thousand', 'fr');
                    result += ' ' + i18n.pluralize('thousand', 'thousands', thousands, 'fr');
                }
                else {
                    // Likewise, I know I want the plural version here, but I kept
                    // this call to pluralize to remain consistent.
                    result += toTextualValue(thousands) + ' ' + i18n.pluralize('thousand', 'thousands', thousands, 'fr');
                }
            }
            if (hundreds >= 1) { // if (3 >= 1)
                // 1 hundred is a special case.  Instead of un cent, it's just cent
                // So we don't call toTextualValue for the 1 in that case
                if (hundreds == 1) {
                    // Again, I know we want the singular version here, but I kept
                    // this call to pluralize to remain consistent
                    result += ' ' + i18n.pluralize('hundred', 'hundreds', hundreds, 'fr');
                }
                else {
                    // Again, I know we want the plural version here, but I kept
                    // this call to pluralize to remain consistent
                    result += toTextualValue(hundreds) + ' ' + i18n.pluralize('hundred', 'hundreds', hundreds, 'fr');
                }
            }
            
            if (tens > 0) { // if (2 > 0)
                if (tens == 1) { // 10 - 19
                    // 10 - 19 are unique
                    result += ' ' + i18n._('~' + tens + ones, 'fr');
                }
                else if (tens < 7) { // 20 - 69
                    // 20 - 69 use a 'tens' value (vingt, trente, etc.) + 1 - 9
                    // so we want to write out the 'tens' value first
                    result += ' ' + i18n._('~' + tens + '0', 'fr');
                }
                else if (tens == 7) { // 70 - 79
                    // Seventy is pronounced sixty + (ten + ones).  For example,
                    // 72 is 60 + 12.  78 is 60 + 18.  So first we want to write out
                    // the 60 'tens' value
                    result += ' ' + i18n._('~' + '6' + '0', 'fr');
                    if (ones == 1) { // 71
                        // 71 is a special case. We say "sixty and eleven" whereas
                        // 72 is just "sixty-twelve"
                        result += ' ' + i18n._('and', 'fr') + ' ';
                    }
                    else { // 70, 72 - 79
                        result += '-';
                    }
                    // We've written out the 'sixty' part, now add 10 + the ones
                    result += i18n._('~' + '1' + ones, 'fr');
                }
                else if (tens == 8 || tens == 9) { // 80 - 99
                    // 4x20 + ones
                    if (tens == 8 && ones == 0) { // NOTE, THIS WAS WRONG, FIXED NOW
                        // 80 is a special case
                        result += i18n._('~' + '80', 'fr');
                    }
                    else {
                        // 81 - 99 is going to be 4-20-(1 through 19)
                        result += i18n._('~' + '4', 'fr') + '-' + i18n._('~' + '20', 'fr') + '-' + i18n._('~' + (tens == 8?'':'1') + ones, 'fr');
                    }
                }
            }
            
            if (ones > 0 && tens != 1 && tens < 7) { // 1 - 9, 21 - 69
                if (tens > 1) { // 21 - 69
                    if (ones == 1) { // 21, 31, 41, 51, 61
                        result += ' ' + i18n._('and', 'fr') + ' ';
                    }
                    else { // 22 - 29, 32 - 39, etc.
                        result += '-';
                    }
                }
                else { // 1 - 9
                    result += ' ';
                }
                result += i18n._('~' + ones, 'fr');
            }
             
            return result;
        }
        
        return {
            toTextDollars : function(amt) {
                if (amt > 999999999.99 || amt < 0) {
                    return i18n._('invalid amount', 'fr');
                }
                var francs = parseInt(amt);
                var centimes = parseInt(Math.round((amt - Math.floor(amt)) * 100));
                return (toTextualValue(francs) + ' ' + 
                        i18n.pluralize('currencyName', 'currencyNamePlural', francs, 'fr') + 
                        ' ' + i18n._('and', 'fr') + toTextualValue(centimes) + ' ' + 
                        i18n.pluralize('fractionalCurrency', 'fractionalCurrencyPlural', centimes, 'fr'));
            }
        };
    }();
    
    var foo = 7654321;
    //var bar = 120031.45
    alert(foo + " :\n\tEnglish: " + enMoneyConverter.toTextDollars(foo) + "\n\tFrench: " + frMoneyConverter.toTextDollars(foo));
    //alert(bar + " :\n\tEnglish: " + enMoneyConverter.toTextDollars(bar) + "\n\tFrench: " + frMoneyConverter.toTextDollars(bar));
    </script>
    
    </div>
    </body>
    </html> 
             
    
    Fotiman 
    View Public Profile 
    Send a private message to Fotiman 
    Find More Posts by Fotiman 
    Add Fotiman to Your Buddy List

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hmm. Tricky one. For example, the number 101 ...

    EN = one hundred and one
    FR = cent et un
    GE = hundert eins

    the number 21 ...

    EN = twenty one
    FR = vingt et un
    GE = ein und zwanzig

    So as you can see, one could by ein or eins depending on context.
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick stab at it. Note, I didn't do anything yet with the hundreds+ cases. I wanted to start with the 0 - 99 case first. Also, in this example, I'm getting all English ('en') values for the numbers (since I don't know German). But once you add your translations it should be easy to replace all instances of 'en' in the new function. Note, I've removed the French version so we can focus on German.

    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset:utf-8">
      <title></title>
    </head>
    <body>
     
    <script type="text/javascript">
    var i18n = function(){
        return {
            /**
             * Return an internationalized text string.  If no language is specified
             * then it will default to English (en).  If a translated string is not
             * found for the language, then the value passed in (the key) will be
             * returned.
             * @param {String} k The key representing the value to be returned.
             * @param {String} l The language identifier of the string to be returned.
             */
            _ : function(k, l) {
                if (typeof(l) == 'undefined' || l == null || l == '') {
                    l = 'en'; // Default to english
                }
                if (typeof(this[k]) != 'undefined' && 
                    typeof(this[k][l]) != 'undefined') {
                    return this[k][l];
                }
                return k;
            },
            /**
             * Return the internationalized singular or plural version of a string
             * @param {String} s The key for the singular version of the string
             * @param {String} p The key for the plural version of the string
             * @param {Number} n The number to use for determining sigularity
             * @param {String} l The language identifier of the string to be returned
             */
            pluralize : function(s, p, n, l) {
                if (n != 1) {
                    return this._(p, l);
                }
                return this._(s, l);
            }
            /* Below are the strings to be translated. Keep alphabetical by key. */
            ,'~0':                  {en: 'zero', fr: 'zéro'}
            ,'~1':                  {en: 'one', fr: 'un'}
            ,'~2':                  {en: 'two', fr: 'deux'}
            ,'~3':                  {en: 'three', fr: 'trois'}
            ,'~4':                  {en: 'four', fr: 'quatre'}
            ,'~5':                  {en: 'five', fr: 'cinq'}
            ,'~6':                  {en: 'six', fr: 'six'}
            ,'~7':                  {en: 'seven', fr: 'sept'}
            ,'~8':                  {en: 'eight', fr: 'huit'}
            ,'~9':                  {en: 'nine', fr: 'neuf'}
            ,'~10':                 {en: 'ten', fr: 'dix'}
            ,'~11':                 {en: 'eleven', fr: 'onze'}
            ,'~12':                 {en: 'twelve', fr: 'douze'}
            ,'~13':                 {en: 'thirteen', fr: 'treize'}
            ,'~14':                 {en: 'fourteen', fr: 'quatorze'}
            ,'~15':                 {en: 'fifteen', fr: 'quinze'}
            ,'~16':                 {en: 'sixteen', fr: 'seize'}
            ,'~17':                 {en: 'seventeen', fr: 'dix-sept'}
            ,'~18':                 {en: 'eighteen', fr: 'dix-huit'}
            ,'~19':                 {en: 'nineteen', fr: 'dix-neuf'}
            ,'~20':                 {en: 'twenty', fr: 'vingt'}
            ,'~30':                 {en: 'thirty', fr: 'trente'}
            ,'~40':                 {en: 'forty', fr: 'quarante'}
            ,'~50':                 {en: 'fifty', fr: 'cinquante'}
            ,'~60':                 {en: 'sixty', fr: 'soixante'}
            ,'~70':                 {en: 'seventy', fr: 'soixante-dix'}
            ,'~71':                 {en: 'seventy one', fr: 'soixante et onze'}
            ,'~72':                 {en: 'seventy two', fr: 'soixante-douze'}
            ,'~80':                 {en: 'eighty', fr: 'quatre-vingts'}
            ,'~90':                 {en: 'ninety', fr: 'quatre-vingt-dix'}
            ,'and' :                {en: 'and', fr: 'et'}
            ,'currencyName' :       {en: 'Dollar', fr: 'Franc'}
            ,'currencyNamePlural' : {en: 'Dollars', fr: 'Francs'}
            ,'fractionalCurrency' : {en: 'cent', fr: 'centime'}
            ,'fractionalCurrencyPlural' : {en: 'cents', fr: 'centimes'}
            ,'hundred' :            {en: 'hundred', fr: 'cent'}
            ,'hundreds' :           {en: 'hundreds', fr: 'cents'}
            ,'invalid amount' :     {en: 'Invalid amount'}
            ,'million' :            {en: 'million', fr: 'million'}
            ,'millions' :           {en: 'millions', fr: 'millions'}
            ,'ten' :                {en: 'ten', fr: 'dix'}
            ,'thousand' :           {en: 'thousand', fr: 'mille'}
            ,'thousands' :           {en: 'thousands', fr: 'mille'}
            ,'twenty' :             {en: 'twenty', fr: 'vingt'}
            ,'twentyone' :          {en: 'twenty one', fr: 'vingt et un'}
        };
    }();
     
    var enMoneyConverter = function() {
        /**
         * Convert a decimal value to textual dollar and cents
         * @param {Float} amt The decimal value
         * @return The textual dollar and cents value
         */
        function toTextualValue(amt) {
            if (amt == 0) {
                return ' ' + i18n._('~0', 'en');
            }            
     
            var result = "";
            var millions = Math.floor(amt / 1000000);
            var m = millions * 1000000;
            var thousands = Math.floor((amt - m) / 1000);
            var th = thousands * 1000;
            var hundreds = Math.floor((amt - (m + th)) / 100);
            var h = hundreds * 100;
            var tens = Math.floor((amt - (m + th + h)) / 10);
            var t = tens * 10;
            var ones = Math.floor((amt - (m + th + h + t)));
     
            if (millions >= 1) {
                result += toTextualValue(millions) + ' ' + i18n._('million', 'en');
            }
            if (thousands >= 1) {
                result += toTextualValue(thousands) + ' ' + i18n._('thousand', 'en');
            }
            if (hundreds >= 1) {
                result += toTextualValue(hundreds) + ' ' + i18n._('hundred', 'en');
            }
            if (tens > 0) {
                if (tens == 1) { // 10 - 19
                    result += ' ' + i18n._('~' + tens + ones, 'en');
                }
                else { // 20 - 99
                    result += ' ' + i18n._('~' + tens + '0', 'en');
                } 
            }
            if (ones > 0 && tens != 1) {
                result += ' ' + i18n._('~' + ones, 'en');
            }
     
            return result;
        }
     
        return {
            toTextDollars : function(amt) {
                if (amt > 999999999.99 || amt < 0) {
                    return i18n._('invalid amount', 'en');
                }
                var dollars = parseInt(amt);
                var cents = parseInt(Math.round((amt - Math.floor(amt)) * 100));
                return (toTextualValue(dollars) + ' ' + 
                        i18n.pluralize('currencyName', 'currencyNamePlural', dollars, 'en') + 
                        ' ' + i18n._('and', 'en') + toTextualValue(cents) + ' ' + 
                        i18n.pluralize('fractionalCurrency', 'fractionalCurrencyPlural', cents, 'en'));
            }
        };
    }();
     
    // French version snipped
     
    var deMoneyConverter = function() {
        /**
         * Convert a decimal value to textual dollar and cents
         * @param {Float} amt The decimal value
         * @return The textual dollar and cents value
         */
        function toTextualValue(amt) {
            if (amt == 0) {
                return ' ' + i18n._('~0', 'en');
            }            
     
            var result = "";
            var millions = Math.floor(amt / 1000000);
            var m = millions * 1000000;
            var thousands = Math.floor((amt - m) / 1000);
            var th = thousands * 1000;
            var hundreds = Math.floor((amt - (m + th)) / 100);
            var h = hundreds * 100;
            var tens = Math.floor((amt - (m + th + h)) / 10);
            var t = tens * 10;
            var ones = Math.floor((amt - (m + th + h + t)));
     
            if (millions >= 1) {
                result += toTextualValue(millions) + ' ' + i18n._('million', 'en');
            }
            if (thousands >= 1) {
                result += toTextualValue(thousands) + ' ' + i18n._('thousand', 'en');
            }
            if (hundreds >= 1) {
                result += toTextualValue(hundreds) + ' ' + i18n._('hundred', 'en');
            }
            if (tens > 1) { // 20 - 99
                if (ones > 0) {
                    result += ' ' + i18n._('~' + ones, 'en');
                    result += ' ' + i18n._('and', 'en');
                }
                result += ' ' + i18n._('~' + tens + '0', 'en');
            }
            else {
                if (tens > 0) {
                    result += ' ' + i18n._('~' + tens + ones, 'en');
                }
                if (ones > 0 && tens != 1) {
                    result += ' ' + i18n._('~' + ones, 'en');
                }
            }
     
            return result;
        }
     
        return {
            toTextDollars : function(amt) {
                if (amt > 999999999.99 || amt < 0) {
                    return i18n._('invalid amount', 'en');
                }
                var dollars = parseInt(amt);
                var cents = parseInt(Math.round((amt - Math.floor(amt)) * 100));
                return (toTextualValue(dollars) + ' ' + 
                        i18n.pluralize('currencyName', 'currencyNamePlural', dollars, 'en') + 
                        ' ' + i18n._('and', 'en') + toTextualValue(cents) + ' ' + 
                        i18n.pluralize('fractionalCurrency', 'fractionalCurrencyPlural', cents, 'en'));
            }
        };
    }();
    var foo = 21;
    //var bar = 120031.45
    //alert(foo + " :\n\tEnglish: " + enMoneyConverter.toTextDollars(foo) + "\n\tFrench: " + frMoneyConverter.toTextDollars(foo));
    //alert(bar + " :\n\tEnglish: " + enMoneyConverter.toTextDollars(bar) + "\n\tFrench: " + frMoneyConverter.toTextDollars(bar));
    alert(foo + " :\n\tGerman: " + deMoneyConverter.toTextDollars(foo));
    </script>
     
    </div>
    </body>
    </html>

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I can help you with the Spanish if you want.

    1 to 20:

    Uno, dos, tres, cuatro, cinco, seis, siete, ocho, nueve, diez, once, doce, trece, catorce, quince, diecis&#233;is, diecisiete, dieciocho, diecinueve, veinte.

    21 to 29:
    correct way:

    veinte y uno, veinte y dos, veinte y tres, etc.

    However, with the 20s people are lazy and this is pretty much the way it is:

    veinti&#250;n, veintid&#243;s, veintitres, veinticuatro, veinticinco, veintiseis, veintisiete, veintiocho, veintinueve

    after that, it is the "normal" way. So:
    30: treinta
    40: cuarenta
    50: cincuenta
    60: sesenta
    70: setenta
    80: ochenta
    90: noventa

    and with these it is always [ten] y [unit]. E.g.:

    45: cuarenta y cinco
    67: sesenta y siete
    88: ochenta y ocho
    52: cincuenta y dos
    99: noventa y nueve

    With the 100s it gets complex again.
    100: cien
    200: doscientos
    300: trescientos
    400: cuatrocientos
    500: quinientos
    600: seiscientos
    700: setecientos
    800: ochocientos
    900: novecientos

    With the 1000s it's easier. 1000 is "mil" and the other thousands follow this rule:
    [[hundreds] [tens] units] ["mil"]
    e.g.:

    4000: cuatro mil
    9000: nueve mil
    14000: catorce mil
    254000: doscientos cincuenta y cuatro mil
    930482: novecientos treinta mil cuatrocientos ochenta y dos

    Millions:
    1m: un mill&#243;n
    2m: dos millones
    3m: tres millones
    ...
    8m: ocho millones
    9m: nueve millones


    so:

    2338671: dos millones trescientos treinta y ocho mil seiscientos setenta y uno
    1083077: un mill&#243;n ochenta y tres mil setenta y siete
    4000301: cuatro millones trescientos uno

  5. #5
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How great are you lot,
    not only do you come back with the javascript answers you also help me with the Spanish (which I don't speak),

    Ian, I do know about this do you think it is possible to use something like the
    French 21 (special rule) ,'twentyone' : {en: 'twenty one', fr: 'vingt et un'},

    Fotiman, main thanks again for all your time, I ain't had time to play yet but will get back soon.

    Raffles, You have help me here because the Spanish must be spelled right,
    which I did not have all the website give the veintiuno,
    and as this if for writing bank cheques it must be right.
    so can you tell me if you was to write a cheque which spelling would be excepted by Spanish banks.

    Best regards

    Steve

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As it happens, I have never written a cheque in Spanish. However... check out this rather brilliant site:

    http://www.chequefacility.org/

    I just found it and it seems like it should help you a lot!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •