Newsgroup: it.comp.lang.javascript
Titolo: sara' banale ma..formattare un numero

Vorrei mettere il segno di separazione delle migliaia in un numero che poi
metto in una casella per visualizzarlo a video.
Come posso parlo in javascript.
Questo mi serve per un convertitore euro- lira di cui vi posto il codice:
Grazie.
Beppe
<script Language="javascript">
<!--
var val50 =1936.27
var dec5 =2
var val51 =(1/val50)
var dec =2
function kommaclean(string){
        i=string.indexOf(",");
        while(i != -1)
        {
                string = string.substring(0,i) + '.' + string.substring(i+1,
string.length);
                i=string.indexOf(",");
        }
        i=string.lastIndexOf(".");
        j=string.indexOf(".")
        while(j != i)
        {
                string = string.substring(0,j) + string.substring(j+1,
string.length);
                i=string.lastIndexOf(".");
                j=string.indexOf(".");
        }
        return string;
}
function convert(f){
        invalstring = f.input.value
        invalstring = kommaclean(invalstring)
        inval = parseFloat(invalstring);
        if (f.direction.value=='fromeuro'){
                decuit = 3;
                decin  = 3
;
        }else{
                decuit = 3;
                decin  = 3;
        }
        inval = parseFloat(formatfloat(inval, decin))
        if (!(inval>0)){
                uitval = 0;
                inval  = 0;
        }else{
                if (f.direction.value=='fromeuro'){
                uitval = inval * val50 + 0.005;
                }else{
                uitval = inval * val51 + 0.005;
                }
        }
        f.result.value=formatfloat(uitval,decuit);
        f.input.value=formatfloat(inval,decin);
}
dec = 3
function formatfloat(fl, dec){
        str=""+fl;
        i = str.indexOf(".");
        if (i<0){
                i=str.length;
                str=str+".00000000000";
        }else{
                if(i==0){
                        i=1;
                        str="0"+str;
                }else{
                        str=str+"00000000000";
                }
        }
        return str.substring(0,i+dec);
}
//-->
</script>
</head>
<body bgcolor="#ffffff">
<div align="center"><font face="Arial, Helvetica, sans-serif"><b><IMG
height=56 src="euro.gif" width=57>Convertitore
Euro</B></FONT><IMG height=44 src="euro_calc.gif" width=62></DIV>
<form action="Beppe" method="post" onSubmit="convert(this);return false;">
  <p align="center">
    <input type="hidden" name="direction" value="toeuro">
    <b><font face="Arial, Helvetica, sans-serif">Lire: £</FONT></B>
    <input name="input" size="13" style ="WIDTH: 124px; HEIGHT: 22px">
    <b><font face="Arial, Helvetica, sans-serif">Euro: ?</FONT></B>
    <input name="result" size="12" style ="WIDTH: 116px; HEIGHT: 22px">
    <input type="submit" Value="Calcola">
</P></FORM>
<form action="Beppe" method="post" onSubmit="convert(this);return false;">
  <div align="center">
    <input type="hidden" name="direction" value="fromeuro">
    <b><font face="Arial, Helvetica, sans-serif">Euro:</FONT></B> <font
face="Arial, Helvetica, sans-serif"><b>?</B> </FONT>
<input name="input" size="13" style ="WIDTH: 121px; HEIGHT: 22px">
    <b><font face="Arial, Helvetica, sans-serif">Lire:</FONT></B> <font
face="Arial, Helvetica, sans-serif"><b>£</B></FONT>
    <input name="result" size="13" style ="WIDTH: 120px; HEIGHT: 22px">
    <input type="submit" Value="Calcola"> </DIV></FORM>
<div align="center">

function formatnumber(s) {
  for (var
i=(String(s).indexOf(",")+1?String(s).indexOf(","):String(s).length)-3;i>0;i
-=3)
   s=String(s).substring(0,i)+"."+String(s).substring(i,String(s).length);
  return s;
 }