-
Salve a tutti...vorrei riasolvere un grattacapo e vengo subito al dunque:
come potrei riformattare la stampa del totale con qualcosa del tipo 1.300,00 in quanto che per ora mi da 1300.00 quandi inserire il PUNTO?
Posto la parte di codice che uso......
$('#invia').click(function() { var valore=$('#codice').val(); var valore2=$('#codicet').val(); var valore3=$('#quantita').val(); var valore4=$('#prezzo').val(); var valore5=$('#sconto').val(); var a=Math.round(100*Math.random())+300; totale=parseFloat(totale)+(parseFloat(valore4)*parseInt(valore3)); $('#miatabella').append("<tr id=\"aaa"+a+"\"><td class=\"textDisplay1\"><input type=\"hidden\" name=\"riga"+a+"\" value=\""+valore+"|"+valore2+"|"+valore3+"|"+valore4+"|"+valore5+"\">"+valore+"</td><td class=\"textDisplay1\">"+valore2+"</td><td class=\"textDisplay1\"><font size=6>"+valore3+"</font></td><td class=\"textDisplay1\">"+valore5+"</td><td class=\"textDisplay1\"><font size=6>"+valore4+"</font></td><td class=\"textDisplay1\"><a href=\"javascript:void(0);\" onclick=\"javascript:remove('aaa"+a+"','"+valore3+"','"+valore4+"','"+totale+"')\">Cancella</a></td></tr>"); $('#ytotale').val(totale); perinps=totale*parseFloat(percinps)/100; $('#yinps').val(perinps.roundTo(2)); imponibile=perinps+totale; $('#yimponibile').val(imponibile.roundTo(2)); iva2=imponibile*perciva/100; $('#yiva').val(iva2.roundTo(2)); totalefatt=iva2+imponibile; $('#yfattura').val(totalefatt.roundTo(2)); ritenuta2=imponibile*percritenuta/100; $('#yritenuta').val(-ritenuta2.roundTo(2)); netto2= totalefatt-ritenuta2; $('#ynetto').val(netto2.roundTo(2)); });grazie anticipatamente a tutti ciaooo!!
-
Ad es. usa la variabile.toFix(2) per aggiungere le 2 cifre decimali alla variabile, oppure vediamo altro dopo
-
potresti farmi un esempio concreto...non sono cosi bravo!
-
Ciao, postami la funzione ad es.function roundTo(decimalpositions).
-
//arrotonda a decimalpositions cifre decimali
function roundTo(decimalpositions)
{
var i = this * Math.pow(10,decimalpositions);
i = Math.round(i);
return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo; -
Per avere una più "ampia" visione.
Implementazione:
function roundTo(decimalpositions)
{
var i = this * Math.pow(10,decimalpositions);
i = Math.round(i);
return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo;Ora puoi richiedere a qualunque numero l'arrotondamento ad un dato numero di cifre decimali:
var x = 3.14159;
alert( x.roundTo(2) ); // 3.14
alert( x.roundTo(4) ); // 3.1416Se invece vuoi utilizzare la funzionalità in modalità "stand-alone", devi apportare delle piccole modifiche allo script suddetto e segnatamente:
function roundTo(value, decimalpositions)
{
var i = value * Math.pow(10,decimalpositions);
i = Math.round(i);
return i / Math.pow(10,decimalpositions);
}che richiamerai così:
var x = 4.24159;
alert( roundTo(x, 2) ); // 4.24
alert( roundTo(x, 4) ); // 4.2416 -
scusa ma io in realtà non devo arrotondare il numero...ma inserire solo il punto ai decimali!
Tipo che quando vado a fare la somma dei campi es. 100 ero + 1000 euro mi deve dare 1.100 e non 1100
-
Hai ragione, ma il codice che ti ho postato come esempio fa entrambe due le cose.
-
chettepossino..ma fammi un esempio...non sono capace!!!
sto facendo delle prove con questo numberFormat154.js
function roundTo()
{
var obj = document.getElementById('prezzo');
var num = new NumberFormat();
num.setInputDecimal(',');
num.setNumber(obj.value); // obj.value is '12305'
num.setPlaces('2', false);
num.setCurrencyValue('');
num.setCurrency(true);
num.setCurrencyPosition(num.LEFT_OUTSIDE);
num.setNegativeFormat(num.PARENTHESIS);
num.setNegativeRed(true);
num.setSeparators(true, '.', ',');
obj.value = num.toFormatted();}
..ma non riesco ad applicarlo a dovere!!!
-
Ancora un esempio:
var num = new NumberFormat (20801.275) toFormatted ().;
/ / Numero sarà uguale a 20,801.28 -
Web Designer
Ancora un esempio:
var num = new NumberFormat (20801.275) toFormatted ().;
/ / Numero sarà uguale a 20,801.28ok..ora ci lavoro!
-
nulla...non so come inserirla..mi da errore
-
Per fare prima, usa questo form e poi fammi sapere: www(.)mredkj.com/javascript/numberFormatPage2.html
-
infatti ho provato propio quello ma non so come integrarlo...
questo è quello che fa perme:var num = new NumberFormat();
num.setInputDecimal(',');
num.setNumber(obj.value); // obj.value is '1200'
num.setPlaces('2', false);
num.setCurrencyValue('$');
num.setCurrency(false);
num.setCurrencyPosition(num.RIGHT_OUTSIDE);
num.setNegativeFormat(num.LEFT_DASH);
num.setNegativeRed(false);
num.setSeparators(true, '.', ',');
obj.value = num.toFormatted();
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi