Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2013, 07:13 PM   PM User | #1
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Best method???!!!

Hi guy!!!
That is a method for write this badly string:
Code:

if (arrayNumber[1] == "1" && arrayNumber[2] == "1" || arrayNumber[2] == "2" || arrayNumber[2] == "3" || arrayNumber[2] == "4" || arrayNumber[2] == "5" || arrayNumber[2] == "6" || arrayNumber[2] == "7" ||arrayNumber[2] == "8" || arrayNumber[2] == "9")
in an other string more beautifull ???

Last edited by triko; 01-09-2013 at 07:28 PM..
triko is offline   Reply With Quote
Old 01-09-2013, 07:35 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
You didn't increment the array index past 2 in your example; also, the first position in a JavaScript array is 0, not 1.

Code:
if((arrayNumber[0] == "1" && arrayNumber[1] == "2") ||
   (arrayNumber[1] == "3" && arrayNumber[2] == "3") ||
...
   (arrayNumber[9] == "10" && arrayNumber[10] == "11")){
Also, these are evaluating as strings, not integers. Did you want integer values?
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 01-09-2013, 08:05 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Rewriting *ONLY* what you showed there, not attempting to extend it as WolfShade did:
Code:
var s = arrayNumber[1] + arrayNumber[2];
if ( ( /^1[1-9]$/ ).test(s) )
{
    // pattern is 1 followed by any digit except 0
} else {
   // pattern is not matched
}
Maybe if you *DESCRIBED* what you want instead of writing bad JS code we could help you more?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-11-2013, 12:04 PM   PM User | #4
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Yes, you're right!!
In my problem i do translate from number ex : 125020 to letters, one hundred twenty-five thousand and twenty!
And i use more if and put an condition if number from 1 to 9
But can you explain me what means this symbol ? : if ( ( /^1[1-9]$/ ).test(s) )
triko is offline   Reply With Quote
Old 01-11-2013, 12:42 PM   PM User | #5
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Try this for number to 1000...
Code:
<body>
<h2>«&nbsp;How to spell numbers ?&nbsp;»</h2>
<p><input type="text" id="ntr" onkeyup="readNmb(this.value)"></p>
<p id="rsp"></p>
<script type="text/javascript">
function $(i) {return document.getElementById(i)};

function nmb(n){var c,m,r;
	c='';r=0;s=0;
 	if (100<=n) {m=Math.floor(n/100);c+=nmb(m)+' hundred';n-=m*100}
	if (90<=n) {c+=' ninety';n-=90;}
	if (80<=n) {c+=' eighty';n-=80;}
	if (70<=n) {c+=' seventy';n-=70;}
	if (60<=n) {c+=' sixty';n-=60;}
	if (50<=n) {c+=' fifty';n-=50;}
	if (40<=n) {c+=' forty';n-=40}
	if (30<=n) {c+=' thirty';n-=30;}
	if (20<=n) {c+=' twenty';n-=20}
	if (0==n) return c;
	else if (c && n<10) c+='-';
	return c+' '+'zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen'.split(',')[n];
}
function readNmb(v){
	$('rsp').innerHTML=nmb(v).replace(/- /g,'-');
}
</script>
</body>
</html>
Then try to cut bigger numbers...
007julien is offline   Reply With Quote
Old 01-11-2013, 08:37 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by triko View Post
In my problem i do translate from number ex : 125020 to letters, one hundred twenty-five thousand and twenty!
So what is the point in testing just the FIRST TWO characters, as your ugly code was trying to do?
Quote:
But can you explain me what means this symbol ? : if ( ( /^1[1-9]$/ ).test(s) )
/ ==>> beginning of a regular expression
^ ==>> beginning of text (that is, nothing can appear before what we are testing)
1 ==>> the digit 1, only. Nothing else allowed.
[1-9] ==>> any digit from 1 to 9
$ ==>> the end of the text (nothing can appear after what we are testing)
/ ==>> the end of the regular expression

In short, that says "Look for text that has a 1 as the first character and a 1 through 9 as the second character and is exactly 2 characters long."

But who cares? It's entirely irrelevant to what you really need.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-11-2013, 09:03 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Is this what you are trying to accomplish?
Code:
<!DOCTYPE html>
<html>
<head>
<title>Numbers to words</title>
<style type="text/css">
* { font-size: x-large; }
</style>
</head>
<body>
<div>
<form>
    Enter a number: <input id="theNumber" />
    <hr />
    That number in words:<br />
       <span id="words"></span>
</form>
</div>

<script type="text/javascript">
var UnitNames = [" zero"," one"," two"," three"," four", 
                 " five"," six"," seven"," eight"," nine" ];
var TeenNames = [" ten"," eleven"," twelve"," thirteen"," fourteen", 
                 " fifteen"," sixteen"," seventeen"," eighteen"," nineteen" ];
var DecadeNames = [" zero"," ten"," twenty"," thirty"," forty", 
                 " fifty"," sixty"," seventy"," eighty"," ninety" ];

document.getElementById("theNumber").onchange =
    function( ) 
    {
        var text = document.getElementById("words");
        text.innerHTML = NumberAsWord( parseInt(this.value) );
    };

function NumberAsWord( num )
{
    
    var millions, thousands, hundreds, decades, result;

    if ( isNaN(num) )
    {
        return "<i>That is NOT a valid number!</i>";
    }
    if ( num == 0 )
    {
        return "zero";
    }
    result = "";
    if ( num < 0 ) 
    {
        num = - num;
        result = "<i>NEGATIVE</i> ";
    }
    millions = Math.floor( num / 1000000 );
    num %= 1000000;
    if ( millions > 0 )
    {
        if ( millions > 999 )
        {
            return "BILLIONS and BILLIONS";
        }
        result += NumberAsWord( millions ) + " million"
        if ( num == 0 ) return result;
    }
    
    thousands = Math.floor( num / 1000 );
    num %= 1000;
    if ( thousands > 0 )
    {
        result += NumberAsWord( thousands ) + " thousand"
        if ( num == 0 ) return result;
    }
    hundreds = Math.floor( num / 100 );
    num %= 100;
    if ( hundreds > 0 )
    {
        result += UnitNames[ hundreds ] + " hundred"
    }
    decades = Math.floor( num / 10 );
    num %= 10;
    if ( decades == 1 )
    {
        result += TeenNames[ num ];
    } else {
        if ( decades > 1 )
        {
            result += DecadeNames[ decades ];
        }
        if ( num > 0 )
        {
            result += UnitNames[ num ];
        }
    }
    return result;
}
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-11-2013 at 09:06 PM..
Old Pedant is offline   Reply With Quote
Old 01-12-2013, 12:00 AM   PM User | #8
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Is this what you are trying to accomplish?
Code:
<!DOCTYPE html>
<html>
<head>
<title>Numbers to words</title>
<style type="text/css">
* { font-size: x-large; }
</style>
</head>
<body>
<div>
<form>
    Enter a number: <input id="theNumber" />
    <hr />
    That number in words:<br />
       <span id="words"></span>
</form>
</div>

<script type="text/javascript">
var UnitNames = [" zero"," one"," two"," three"," four", 
                 " five"," six"," seven"," eight"," nine" ];
var TeenNames = [" ten"," eleven"," twelve"," thirteen"," fourteen", 
                 " fifteen"," sixteen"," seventeen"," eighteen"," nineteen" ];
var DecadeNames = [" zero"," ten"," twenty"," thirty"," forty", 
                 " fifty"," sixty"," seventy"," eighty"," ninety" ];

document.getElementById("theNumber").onchange =
    function( ) 
    {
        var text = document.getElementById("words");
        text.innerHTML = NumberAsWord( parseInt(this.value) );
    };

function NumberAsWord( num )
{
    
    var millions, thousands, hundreds, decades, result;

    if ( isNaN(num) )
    {
        return "<i>That is NOT a valid number!</i>";
    }
    if ( num == 0 )
    {
        return "zero";
    }
    result = "";
    if ( num < 0 ) 
    {
        num = - num;
        result = "<i>NEGATIVE</i> ";
    }
    millions = Math.floor( num / 1000000 );
    num %= 1000000;
    if ( millions > 0 )
    {
        if ( millions > 999 )
        {
            return "BILLIONS and BILLIONS";
        }
        result += NumberAsWord( millions ) + " million"
        if ( num == 0 ) return result;
    }
    
    thousands = Math.floor( num / 1000 );
    num %= 1000;
    if ( thousands > 0 )
    {
        result += NumberAsWord( thousands ) + " thousand"
        if ( num == 0 ) return result;
    }
    hundreds = Math.floor( num / 100 );
    num %= 100;
    if ( hundreds > 0 )
    {
        result += UnitNames[ hundreds ] + " hundred"
    }
    decades = Math.floor( num / 10 );
    num %= 10;
    if ( decades == 1 )
    {
        result += TeenNames[ num ];
    } else {
        if ( decades > 1 )
        {
            result += DecadeNames[ decades ];
        }
        if ( num > 0 )
        {
            result += UnitNames[ num ];
        }
    }
    return result;
}
</script>
</body>
</html>
:O :O :O :O :O :O :O
Don ' t have word!!! Very unbelievable program.... You are a pro!!! my program is very bad....
and don t work well!!!
Code:
<!DOCTYPE>
<html>
    <head><title>Converter</title>
        <script>
            function convert ()
            {
                var final = "";
                var foo = 0;
                var text = document.getElementById("number").value;
                var len = text.length;
                var arrayUnità = ["", "uno" , "due" , "tre", "quattro", "cinque", "sei", "sette", "otto", "nove"];
                var arrayPrimi = ["", "undici", "dodici", "tredici", "quattordici", "quindici", "sedici", "diciassette", "diciotto", "diciannove"];
                var arrayDecine = ["", "dieci", "venti", "trenta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta"];
                var cento = "cento";
                var mille = "mille";
                var mila = "mila";
                var arrayNumber = [];
                for (i = 0; i < len; i ++)
                {
                    arrayNumber[foo] = text[foo];
                    foo++;
                }
                for (i = 0; i < len; i++)
                {
                    if (len == 4)
                    {
                        for (a = 0; a < len; a++)
                        {
                            if (arrayNumber[0] == "1" && arrayNumber[1] == "1" && arrayNumber[2] == "1")
                            {
                                var f = arrayNumber[a];
                                final = mille + cento;
                                a = 3;
                                var f = arrayNumber[a];
                                final += arrayPrimi[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] == "1" && arrayNumber[1] != "1" && arrayNumber[2] != "1")
                            {
                                final = mille;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] == "1" && arrayNumber[1] != "1" && arrayNumber [2] == "1" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
                            {
                                final = mille;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f] + cento;
                                a = 3;
                                var f = arrayNumber[a];
                                final += arrayPrimi[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] == "1" && arrayNumber[1] == "1" && arrayNumber[2] != "1")
                            {
                                final = mille + cento;
                                a = 2;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[1] == "1" && arrayNumber[2] == "1")
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila + cento;
                                a = 3;
                                var f = arrayNumber[a];
                                final += arrayPrimi[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[1] == "1" && arrayNumber[2] != "1")
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila + cento;
                                a = 2;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] == "0" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila;
                                a = 3;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] != "1")
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila;
                                a = 2;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] != "1" && arrayNumber[1] == "0" && arrayNumber[2] == "1" && (arrayNumber[3] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila;
                                a = 2;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayPrimi[f];
                                alert (final);
                                return;
                            }
                            else
                            {
                                var f = arrayNumber[a];
                                final = arrayUnità[f] + mila;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f] + cento;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert (final);
                            }
                        }
                    }
                    if (len == 3)
                    {
                        for (a = 0; a < len; a++)
                        {
                            if (arrayNumber[1] == "1" && (arrayNumber[2] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
                            {
                                final = cento;
                                a = 2;  
                                var f = arrayNumber[a];
                                final += arrayPrimi[f];
                                alert (final);
                                return;
                            }
                            if (arrayNumber[0] == "1")
                            {
                                 // ora è 9, ad ogni ciclo passa al numero dopo!!
                                final = cento;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                aler(final);
                                return;
                            }
                            else
                            {
                                var f = arrayNumber[a] // ora è 9, ad ogni ciclo passa al numero dopo!!
                                final = arrayUnità[f] + cento;
                                a++;
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a];
                                final += arrayUnità[f];
                                alert(final);
                                return;
                            }
                        }
                    }
                    if (len == 2)
                    {
                        for (a = 0; a < len; a++)
                        {
                            if (arrayNumber[0] == 1 && (arrayNumber[2] == "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9"))
                            {
                                a++;
                                var f = arrayNumber[a];
                                final = arrayPrimi[f];
                            }
                            else
                            {
                                var f = arrayNumber[a];
                                final += arrayDecine[f];
                                a++;
                                var f = arrayNumber[a]
                                final += arrayUnità[f];
                            }
                            alert (final);
                        }
                    }
                    if (len == 1)
                    {
                        if (arrayNumber[0] == "0")
                        {
                            alert ("zero");
                        }
                        else
                        {
                            a = 0;
                            var f = arrayNumber[a];
                            final = arrayUnità[f];
                            alert (final);
                        }
                    }
                    len = 0;
                }
            }
        </script>
    </head>
    <body>
        <p>Insert number of max 4 character</p>
        <input type = "text" id = "number" /> <br /><br />
        <button type="button" onclick="convert ()"> CONVERT TO STRING </button>
    </body>
</html>
triko is offline   Reply With Quote
Old 01-12-2013, 12:10 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The rules for English numbers are surely different than the rules for Italian numbers, but I would think you can find a way to adapt my code.

I do see that Italian treats 10 through 19 differently than 20 through 99, same as English, so it might not be too hard to adapt.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-12-2013, 11:03 AM   PM User | #10
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
A variant with hyphens (in line on this page) :
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr">
<head>
<title>Number's literal shapes</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
body,input {font-family:georgia;font-size:medium;color:#000066;}
body {margin:0px;padding:20px;}
#pge {display:block;width:600px;margin:0 auto;padding:7px;}
td {padding:7px;font-size:small;text-align:top}
input{text-align:center;font-family:georgia;font-size:small;}
#rsp {font-size:medium;}
</style>
</head>
<body>
<div id="pge"><fieldset><legend>Number's literal shapes</legend>
<table>
	<tr><td><input type="text" id="ntr" maxlength="15" onkeyup="readNmb(this.value)">
		<br><input type="radio" id="shc" name="rdb" checked="checked" onclick="readNmb($('ntr').value)">&nbsp;Short scale</br>
		<input type="radio" name="rdb" onclick="readNmb($('ntr').value)">&nbsp;Long scale</td>
		<td id="rsp"></td></tr></table>
</fieldset>
</div>
<script type="text/javascript">
function $(i) {return document.getElementById(i)};

function triplets(n){var c='',m;
 	if (100<=n) {m=Math.floor(n/100);c+=triplets(m)+' hundred';n-=m*100}
	n.toString().replace(/^[2-9](?=\d)/,function(a){
		c+=' '+('twenty,thirty,forty,fifty,sixty,seventy,eighty,ninety'.split(',')[a-2]);n-=(+a)*10});
	if (n==0) return c;else if (c && n<10) c+='-';
	return c+' '+'one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen'.split(',')[n-1];
}
function anyNumber(n){var lng,isS;
	wds=($('shc').checked?' ,thousand,million,billion,trillion':' ,thousand,million,milliard,billion').split(',');
	n.replace(/(\d{1,3})(?=((\d\d\d)*)$)/g,function(a,b,c){ 
		 if (c && c!='undefined') lng=c.toString().length/3;else lng=0;
		 if (isS=triplets(a.replace(/^0+/,''))) $('rsp').innerHTML+='<br>'+isS.replace(/- /g,'-')+' '+wds[lng];
	});
}
function readNmb(w){
	if (!w) $('rsp').innerHTML=''; 
	else {$('rsp').innerHTML='<b>'+w.replace(/(\d)(?=(\d\d\d)+$)/g,'$1,')+'</b> is written :';
		w=w.replace(/^0+/,'');
		if (!w) $('rsp').innerHTML+='<br>zero'; 
		else anyNumber(w);}
}
$('ntr').focus();
</script>
</body>
</html>
EDIT : An essay with Italian...
Code:
<body>
<div id="pge"><fieldset><legend>Italian numbers</legend>
<table>
	<tr><td><input type="text" id="ntr" maxlength="15" onkeyup="readNmb(this.value)">
		<td id="rsp"></td>
	</tr></table>
</fieldset>
</div>
<script type="text/javascript">
function $(i) {return document.getElementById(i)};

function triplets(n){var c='',m;
 	if (100<=n) {m=Math.floor(n/100);c+=triplets(m)+'cento';n-=m*100}
	n.toString().replace(/^[2-9](?=\d)/,function(a){
		c+='venti,trenta,quaranta,cinquanta,sessanta,settenta,ottanta,novanta'.split(',')[a-2];n-=(+a)*10});
	if (n==0) return c;
	return c+'uno,due,tre,quattro,cinque,sei,sette,otto,nove,dieci,undici,dodici,tredici,quattrodici,quindici,sedici,diciasette,diciotto,dicianove'.split(',')[n-1];
}
function anyNumber(n){var lng,trp,isS,chn='',m;
	n.replace(/(\d{1,3})(?=((\d\d\d)*)$)/g,function(a,b,c){ 
	    if (c && c!='undefined') lng=c.toString().length/3;else lng=0;
	    isS=triplets(m=a.replace(/^0+/,''));
	    trp=' ,mille,milione,miliardo,bilione'.split(',')[lng];
		 if (1<m) trp=trp.replace(/mille/,'milla');
		 if (isS) chn+=' '+isS+trp;
	});
	$('rsp').innerHTML+=chn.replace(/\wtre$/g,'$1tré').replace(/(a|i|u|o)(a|o)/g,'$2');
}
function readNmb(w){
	if (!w) $('rsp').innerHTML=''; 
	else {$('rsp').innerHTML='<b>'+w.replace(/(\d)(?=(\d\d\d)+$)/g,'$1.')+'</b> si scritti :<br>';
		w=w.replace(/^0+/,'');
		if (!w) $('rsp').innerHTML+='zero'; 
		else anyNumber(w);}
}
$('ntr').focus();
</script>
</body>

Last edited by 007julien; 01-14-2013 at 10:49 AM.. Reason: complements
007julien is offline   Reply With Quote
Old 01-13-2013, 05:16 PM   PM User | #11
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
.. O god!! Julien, this code is suicide for me!!! Don't understand nothing.. it's for high programmers quality!! But thanks
triko is offline   Reply With Quote
Old 01-13-2013, 05:17 PM   PM User | #12
triko
New Coder

 
Join Date: Oct 2012
Location: Italy
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
triko is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
The rules for English numbers are surely different than the rules for Italian numbers, but I would think you can find a way to adapt my code.

I do see that Italian treats 10 through 19 differently than 20 through 99, same as English, so it might not be too hard to adapt.
OK Pedant, I will try to adapt! Thanks
triko is offline   Reply With Quote
Old 01-13-2013, 07:12 PM   PM User | #13
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
The proposed code treats strings rather than numbers. The function anyNumber cuts simply and logically the numbers in three digits slices with a replace method (the regular expression captures each one to three digit slice, before a multiple of three digits) not to replace this slices with return values, but only to call the function for triplets and to define the future response chn...

EDIT : Change the chn.replace(/tre$/g,'tré') with a chn.replace(/(\w)tre\b/g,'$1tré') to replace tre by tré only at the end of a word (after a letter).

Last edited by 007julien; 01-14-2013 at 09:04 AM.. Reason: errors
007julien is offline   Reply With Quote
Old 01-15-2013, 01:51 PM   PM User | #14
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Just an essay to count in Europa... It still lacks many languages (and there are probably still errors) !
007julien is offline   Reply With Quote
Old 01-15-2013, 08:25 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by 007julien View Post
Just an essay to count in Europa... It still lacks many languages (and there are probably still errors) !
Very nice! Now let's see it in Japanese. <grin/>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:26 PM.


Advertisement
Log in to turn off these ads.