SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 34 of 34
  1. #26
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Fotiman

    Sorry for not getting back and yes I tried all the scripts, there was a small problem with your script in that if you entered:
    alert(MoneyConverter.toTextDollars(1.01, "en")); it would return 1 dollars and 1 cents and not the singular dollar and cent,
    and it is easer (for me) to have separate scripts for each language.

    I've come across this script which looks like it could be changed but it has no fractions (cents) and no dollar or dollars but it looks easy to add any excepts to it.

    any thoughts !

    Steve.

    Code:
    <script type="text/javascript">
    function printnumber (printnumber) {
    	var strTemp = '';
    
    	if (printnumber > 99) {
    		firstnumber = printnumber.substr(0,1);
    		middlenumber = printnumber.substr(1,1);
    		lastnumber = printnumber.substr(2,1);
    	} else if (printnumber > 9) {
    		firstnumber = 0;
    		middlenumber = printnumber.substr(0,1);
    		lastnumber = printnumber.substr(1,1);
    	} else {
    		firstnumber = 0;
    		middlenumber = 0;
    		lastnumber = printnumber.substr(0,1);
    	}
    	if (firstnumber != 0) {
    		if (firstnumber == 9) {
    			strTemp = 'nine';
    		} else if (firstnumber == 8) {
    			strTemp = 'eight';
    		} else if (firstnumber == 7) {
    			strTemp =  'seven';
    		} else if (firstnumber == 6) {
    			strTemp = 'six';
    		} else if (firstnumber == 5) {
    			strTemp = 'five';
    		} else if (firstnumber == 4) {
    			strTemp = 'four';
    		} else if (firstnumber == 3) {
    			strTemp = 'three';
    		} else if (firstnumber == 2) {
    			strTemp = 'two';
    		} else if (firstnumber == 1) {
    			strTemp = 'one';
    		}
    		strTemp = strTemp + ' hundred ';
    	}
    	if (middlenumber != 0) {
    		if (middlenumber == 9) {
    			strTemp = strTemp + 'ninety-';
    		} else if (middlenumber == 8) {
    			strTemp = strTemp + 'eighty-';
    		} else if (middlenumber == 7) {
    			strTemp = strTemp + 'seventy-';
    		} else if (middlenumber == 6) {
    			strTemp = strTemp + 'sixty-';
    		} else if (middlenumber == 5) {
    			strTemp = strTemp + 'fifty-';
    		} else if (middlenumber == 4) {
    			strTemp = strTemp + 'fourty-';
    		} else if (middlenumber == 3) {
    			strTemp = strTemp + 'thirty-';
    		} else if (middlenumber == 2) {
    			strTemp = strTemp + 'twenty-';
    		} else if (middlenumber == 1) {
    			if (lastnumber == 9) {
    				strTemp = strTemp + 'nineteen';
    			} else if (lastnumber == 8) {
    				strTemp = strTemp + 'eighteen';
    			} else if (lastnumber == 7) {
    				strTemp = strTemp + 'seventeen';
    			} else if (lastnumber == 6) {
    				strTemp = strTemp + 'sixteen';
    			} else if (lastnumber == 5) {
    				strTemp = strTemp + 'fifteen';
    			} else if (lastnumber == 4) {
    				strTemp = strTemp + 'fourteen';
    			} else if (lastnumber == 3) {
    				strTemp = strTemp + 'thirteen';
    			} else if (lastnumber == 2) {
    				strTemp = strTemp + 'twelve';
    			} else if (lastnumber == 1) {
    				strTemp = strTemp + 'eleven';
    			} else {
    				strTemp = strTemp + 'ten';
    			}
    		}
    	}
    	if (middlenumber != 1) {
    		if (lastnumber == 9) {
    			strTemp = strTemp + 'nine';
    		} else if (lastnumber == 8) {
    			strTemp = strTemp + 'eight';
    		} else if (lastnumber == 7) {
    			strTemp = strTemp + 'seven';
    		} else if (lastnumber == 6) {
    			strTemp = strTemp + 'six';
    		} else if (lastnumber == 5) {
    			strTemp = strTemp + 'five';
    		} else if (lastnumber == 4) {
    			strTemp = strTemp + 'four';
    		} else if (lastnumber == 3) {
    			strTemp = strTemp + 'three';
    		} else if (lastnumber == 2) {
    			strTemp = strTemp + 'two';
    		} else if (lastnumber == 1) {
    			strTemp = strTemp + 'one';
    		}
    	}
    
    	return strTemp;
    }
    
    function AmountInText(value)
    {
    	var number = value;
    	var numarray = number.split('.');
    	numarray[0] = FormatValue(numarray[0]);
    	var dollars = numarray[0].split(',');
    	var strAmountInText = '';
    
    	if (dollars[2]) {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    		strAmountInText = strAmountInText + ' million ';
    		strAmountInText = strAmountInText + printnumber(dollars[1]);
    		strAmountInText = strAmountInText + ' thousand ';
    		strAmountInText = strAmountInText + printnumber(dollars[2]);
    	} else if (dollars[1]) {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    		strAmountInText = strAmountInText + ' thousand ';
    		strAmountInText = strAmountInText + printnumber(dollars[1]);
    	} else {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    	}
    
    	if (!numarray[1]){numarray[1] = "00"}
    	if (strAmountInText == ''){strAmountInText = "zero"}
    
    
    	strAmountInText = strAmountInText + ' dollars, ' + numarray[1] + ' cents';
    
    	return strAmountInText;
    }
    
    function FormatValue(value)
    {
    	var numarray = value.split('.');
    	var strValue = numarray[0]
    	var strTemp = '';
    
    	for (var i = 1; i <= strValue.length; i++) {
    		if (i == 4){
    			strTemp = ',' + strTemp;
    		} else if (i == 7){
    			strTemp = ',' + strTemp;
    		}
    
    		strTemp = Left(Right(strValue, i), 1) + strTemp;
    	}
    
    	return strTemp;
    }
    
    function Left(str, n){
    	if (n <= 0)
    		return "";
    	else if (n > String(str).length)
    		return str;
    	else
    		return String(str).substring(0,n);
    }
    
    function Right(str, n){
    	if (n <= 0)
    	return "";
    	else if (n > String(str).length)
    	return str;
    	else {
    	var iLen = String(str).length;
    	return String(str).substring(iLen, iLen - n);
    	}
    }
    </script>
    <form id="test" name="test"><input type="text" name="inum" id="inum" value="">
    
    <input type="button" value="to dollars" onclick="document.getElementById('rnum').value = printnumber(document.getElementById('inum').value);"><br><br>
    
    <textarea name="rnum" id="rnum" cols="40" rows="5"></textarea></form>
    
    
    </div>
    </body>
    </html>

  2. #27
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a good article on internationalization that might be of help to you:
    http://24ways.org/2007/javascript-internationalisation

  3. #28
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Fotiman

    I've had a quick read, and will go back later and go though it again when I've a bit more time, the Javascript is over my head, but I sort of understand a bit of what he is doing,

    I've been pressing on with your script and sorted a fair bit but having problems sorting out a couple of things,

    in French 71 to 79 is the equivalent of saying 60+11 (71), 60+12 (72), 60+13 (73) etc, ( don't blame me I didn't make it up )
    anyway having a few problems getting this to work, I'll try a bit longer if I have too much trouble I come back here.

    Many Thanks

    Steve

    Steve

  4. #29
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Steve,

    I took a quick poke at revamping my original script to function more like what you'd like. The changes include:

    1. I created an i18n 'object' to handle the text translations (internationalization) and handling singular vs. plural strings (based very much on that article I linked to above but with some minor changes to minimize the litter in the global namespace). I *think* all of my translations are correct, but you should probably verify them.

    2. I created separate global objects (enMoneyConverter and frMoneyConverter) for each language. You could put each of these in their own file if you like.

    3. I was able to reduce the methods down to a single public method (toTextDollars), and make the helper methods private (this is just good OO design). The private methods (toTextualValue) contain the bulk of the language dependent logic.

    I'm not sure if the French logic is 100&#37; correct. My French is a bit rusty. But this might get you going in the right direction. The logic for each language would be self contained so you could easily add new languages without having to modify the existing language code.

    Let me know what you think.

    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: 'z&#233;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'));
            }
        };
    }();
    
    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');
            }            
    
            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.pluralize('million', 'millions', millions, 'fr');
            }
            if (thousands >= 1) {
                if (thousands == 1) {
                    result += ' ' + i18n.pluralize('thousand', 'thousands', thousands, 'fr');
                }
                else {
                    result += toTextualValue(thousands) + ' ' + i18n.pluralize('thousand', 'thousands', thousands, 'fr');
                }
            }
            if (hundreds >= 1) {
                if (hundreds == 1) {
                    result += ' ' + i18n.pluralize('hundred', 'hundreds', hundreds, 'fr');
                }
                else {
                    result += toTextualValue(hundreds) + ' ' + i18n.pluralize('hundred', 'hundreds', hundreds, 'fr');
                }
            }
            
            if (tens > 0) {
                if (tens == 1) { // 10 - 19
                    result += ' ' + i18n._('~' + tens + ones, 'fr');
                }
                else if (tens < 7) { // 20 - 69
                    result += ' ' + i18n._('~' + tens + '0', 'fr');
                }
                else if (tens == 7) { // 70 - 79
                    // 60 + (10 + ones)
                    result += ' ' + i18n._('~' + '6' + '0', 'fr');
                    if (ones == 1) { // 71
                        result += ' ' + i18n._('and', 'fr') + ' ';
                    }
                    else { // 70, 72 - 79
                        result += '-';
                    }
                    result += i18n._('~' + '1' + ones, 'fr');
                }
                else if (tens == 8 || tens == 9) { // 80 - 99
                    // 4x20 + ones
                    if (tens == 9 && ones == 0) {
                        result += i18n._('~' + '80', 'fr');
                    }
                    else {
                        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 {
                    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 = 0.71;
    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>

  5. #30
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Hey Hey

    Looks very promising, I've had a little play and took a look at the script,
    its great just what I ordered, just for my workings, I would normally have separate scripts, but now I'm all thinking loads of different things,

    I will go though and customize it, not that it will help you but this is what I did with your old script (bearing in mind I'm a virgin javascriptor) with a little bit of HTML coding, you might find it useful

    Now sit down and pour yourself a nice long beer, put your feet up and have a laugh at my attempt that should cheer you up

    Many thanks Fotiman Top coding

    Steve.

    I've marked my bits with // My 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>
      <script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    </head>
    <body>
    <div id="container">
    
    <script type="text/javascript">
    function printnumber (printnumber) {
    	var strTemp = '';
    
    	if (printnumber > 99) {
    		firstnumber = printnumber.substr(0,1);
    		middlenumber = printnumber.substr(1,1);
    		lastnumber = printnumber.substr(2,1);
    	} else if (printnumber > 9) {
    		firstnumber = 0;
    		middlenumber = printnumber.substr(0,1);
    		lastnumber = printnumber.substr(1,1);
    	} else {
    		firstnumber = 0;
    		middlenumber = 0;
    		lastnumber = printnumber.substr(0,1);
    	}
    	if (firstnumber != 0) {
    		if (firstnumber == 9) {
    			strTemp = 'neuf';
    		} else if (firstnumber == 8) {
    			strTemp = 'huit';
    		} else if (firstnumber == 7) {
    			strTemp =  'sept';
    		} else if (firstnumber == 6) {
    			strTemp = 'six';
    		} else if (firstnumber == 5) {
    			strTemp = 'cinq';
    		} else if (firstnumber == 4) {
    			strTemp = 'quatre';
    		} else if (firstnumber == 3) {
    			strTemp = 'trois';
    		} else if (firstnumber == 2) {
    			strTemp = 'deux';
    		} else if (firstnumber == 1) {
    			strTemp = 'un';
    		}
            // My Code: hundred string here (cent)french
     if (firstnumber = 1){strTemp = "cent " ;}
     if (printnumber == 200){strTemp = "deux cents " ;}
     if (printnumber == 300){strTemp = "trois cents " ;}
     if (printnumber == 400){strTemp = "quatre cents " ;}
     if (printnumber == 500){strTemp = "cinq cents " ;}
     if (printnumber == 600){strTemp = "six cents " ;}
     if (printnumber == 700){strTemp = "sept cents " ;}
     if (printnumber == 800){strTemp = "huit cents " ;}
     if (printnumber == 900){strTemp = "neuf cents " ;}
    
       else {
               strTemp + ' hundred ';
             }
     // my Code: end
      }
    
    	if (middlenumber != 0) {
    		if (middlenumber == 9) {
    			strTemp = strTemp + 'quatre-vingt-dix-';
    		} else if (middlenumber == 8) {
    			strTemp = strTemp + 'quatre-vingt-';
    		} else if (middlenumber == 7) {
    			strTemp = strTemp + 'soixante-dix-';
    		} else if (middlenumber == 6) {
    			strTemp = strTemp + 'soixante-';
    		} else if (middlenumber == 5) {
    			strTemp = strTemp + 'cinquante-';
    		} else if (middlenumber == 4) {
    			strTemp = strTemp + 'quarante-';
    		} else if (middlenumber == 3) {
    			strTemp = strTemp + 'trente-';
    		} else if (middlenumber == 2) {
    			strTemp = strTemp + 'vingt-';
    		} else if (middlenumber == 1) {
    			if (lastnumber == 9) {
    				strTemp = strTemp + 'dix-neuf';
    			} else if (lastnumber == 8) {
    				strTemp = strTemp + 'dix-huit';
    			} else if (lastnumber == 7) {
    				strTemp = strTemp + 'dix-sept';
    			} else if (lastnumber == 6) {
    				strTemp = strTemp + 'seize';
    			} else if (lastnumber == 5) {
    				strTemp = strTemp + 'quinze';
    			} else if (lastnumber == 4) {
    				strTemp = strTemp + 'quatorze';
    			} else if (lastnumber == 3) {
    				strTemp = strTemp + 'treize';
    			} else if (lastnumber == 2) {
    				strTemp = strTemp + 'douze';
    			} else if (lastnumber == 1) {
    				strTemp = strTemp + 'onze';
    			} else {
    				strTemp = strTemp + 'dix';
    			}
    		}
    	}
    	if (middlenumber != 1) {
    		if (lastnumber == 9) {
    			strTemp = strTemp + 'neuf';
    		} else if (lastnumber == 8) {
    			strTemp = strTemp + 'huit';
    		} else if (lastnumber == 7) {
    			strTemp = strTemp + 'sept';
    		} else if (lastnumber == 6) {
    			strTemp = strTemp + 'six';
    		} else if (lastnumber == 5) {
    			strTemp = strTemp + 'cinq';
    		} else if (lastnumber == 4) {
    			strTemp = strTemp + 'quatre';
    		} else if (lastnumber == 3) {
    			strTemp = strTemp + 'trois';
    		} else if (lastnumber == 2) {
    			strTemp = strTemp + 'deux';
    		} else if (lastnumber == 1) {
    			strTemp = strTemp + 'un';
    		}
    	}
    
    	return strTemp;
    }
    
    function AmountInText(value)
    {
    	var number = value;
    	var numarray = number.split('.');
    	numarray[0] = FormatValue(numarray[0]);
    	var dollars = numarray[0].split(',');
    	var strAmountInText = '';
    
    	if (dollars[2]) {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    		strAmountInText = strAmountInText + ' million ';
    		strAmountInText = strAmountInText + printnumber(dollars[1]);
    		strAmountInText = strAmountInText + ' milles ';
    		strAmountInText = strAmountInText + printnumber(dollars[2]);
    	} else if (dollars[1]) {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    		strAmountInText = strAmountInText + ' mille ';
    		strAmountInText = strAmountInText + printnumber(dollars[1]);
    	} else {
    		strAmountInText = strAmountInText + printnumber(dollars[0]);
    	}
    
    	if (!numarray[1]){numarray[1] = "00"}
    	if (strAmountInText == ''){strAmountInText = "zero"}
    
    // my code: dollar & dollars / euro & euros
    
        if (strAmountInText == 'un'){strAmountInText = "un euro "} ;
           else {
            strAmountInText += ' euros ';
        }
    // my code: dollar & dollars / euro & euros
    
    
    
    
    	strAmountInText = strAmountInText + numarray[1] + ' cents';
    
    	return strAmountInText;
    }
    
    function FormatValue(value)
    {
    	var numarray = value.split('.');
    	var strValue = numarray[0]
    	var strTemp = '';
    
    	for (var i = 1; i <= strValue.length; i++) {
    		if (i == 4){
    			strTemp = ',' + strTemp;
    		} else if (i == 7){
    			strTemp = ',' + strTemp;
    		}
    
    		strTemp = Left(Right(strValue, i), 1) + strTemp;
    	}
    
    	return strTemp;
    }
    
    function Left(str, n){
    	if (n <= 0)
    		return "";
    	else if (n > String(str).length)
    		return str;
    	else
    		return String(str).substring(0,n);
    }
    
    function Right(str, n){
    	if (n <= 0)
    	return "";
    	else if (n > String(str).length)
    	return str;
    	else {
    	var iLen = String(str).length;
    	return String(str).substring(iLen, iLen - n);
    	}
    }
    </script>
    <form id="test" name="test"><input type="text" name="inum" id="inum" value="">
    
    <input type="button" value="to dollars" onclick="document.getElementById('rnum').value = AmountInText(document.getElementById('inum').value);"><br><br>
    
    <textarea name="rnum" id="rnum" cols="40" rows="5"></textarea></form>
    
    
    </div>
    </body>
    </html>
    I will go back and play a little more now

  6. #31
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (printnumber > 99) {
    But you then only peel off 3 characters... what if the number is 1000?
    Instead, consider dividing the number by multiples of 10 to strip off each digit (in my example, this was the part that started with var millions = Math.floor(amt / 1000000).

    if (firstnumber == 9)
    Now you're comparing a string (firstnumber) with an integer. Another reason to use the Math.floor approach instead. Also, instead of a bunch of if/else if statements, a switch would be more apporopriate (if you decide to go this route).

    if (printnumber == 200){strTemp = "deux cents " ;}
    But what if printnumber is 201 - 299? It's still going to start with deux cents, but you're not accounting for that. Likewise for 300, 400, 500, 600, 700, 800, and 900.

    if (middlenumber == 9) {
    Same comment as with firstnumber. But also, 91 is not supposed to be 'quatre-vingt-dix-un'. It's supposed to be 'quatre-vingt-onze' (80 + 11). So I think your logic is incorrect here.

    -- that was as far as I got... have something else I need to work on. Your actual code looks fine, but I think the logic is not quite right. Take a closer look at what I was doing. I did find a small bug in my French code which I've fixed in this code below. I also added a bunch of comments so you can better understand what the code is doing (and why). Hope this helps.

    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: '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'));
            }
        };
    }();
    
    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>

  7. #32
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Fotiman

    I've had another play with the script, it far the best script, thanks a lot,
    I found the "etzero" problem, but I've change that to display "00" if no cents are entered, and Francs to euros, (you haven't be here for a while )
    and dollars to pounds, pence, penny, and just getting the feel of it,

    the last script is great, I need all the help I can and the comments you put in help a lot, I'll just slim down the final copy and remove most of them,

    I understand what your saying I hold my hands up with the maths part, Its way beyond me, so I was trying to work around it, but it involved ten times more code, with spending a bit of time with this script I might get a bit better at understanding it all,
    I hope you don't mind me keep bugging you too much, I wouldn't want to over stretch your kind help.

    Many thanks again

    Best regards

    Steve.

  8. #33
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    If it helps, his comments have been provided in the javadoc format, which is a way of providing the documentation directly with the code itself.

  9. #34
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I'm Back

    Hi Guys
    I've been busy knocking up my new website with the new script but now need to add two new languages, German which is very much the same as English rules except 20 > 99 are reversed (twenty six = six & twenty).

    the other one is Spanish which is a little harder to do if I start with the German language 1st it might help me get a grasp of how to do the Spanish one.

    German.

    as explained I have to reverse all the numbers between the 20 and 99.
    so 26 = six and twenty, 126 = hundred six and twenty,

    I've tried swapping bits about to try and revers them but just keep getting rubbish back (numbers or blank )
    so can anyone tell me where I should start.

    best regards

    Steve


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
  •