Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/03/2012, 09:28
Avatar de Naahuel
Naahuel
 
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 1 mes
Puntos: 192
Respuesta: Convertir Cantidad a letra

Jejeje, pedís poco! :P

Respecto a lo primero, esta librería funciona bonito: http://josscrowcroft.github.com/accounting.js/ Fijate en el demo!


Con lo segundo, encontré una función bien pequeña que hace un lindo trabajo, pero en inglés lamentablemente. Es probable que puedas convertirla a español. Tal vez no enuncie bien los números pero creo que será bastante entendible:

Código HTML:
Ver original
  1. <!doctype html>
  2.     <title>Numero a letras</title>
  3.     <script type="text/javascript">
  4.         // Convert numbers to words
  5.         // copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
  6.         // permission to use this Javascript on your web page is granted
  7.         // provided that all of the code (including this copyright notice) is
  8.         // used exactly as shown (you can change the numbering system if you wish)
  9.  
  10.         // American Numbering System
  11.         var th = ['','thousand','million', 'billion','trillion'];
  12.         // uncomment this line for English Number System
  13.         // var th = ['','thousand','million', 'milliard','billion'];
  14.  
  15.         var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
  16.         var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
  17.         var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
  18.         function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}
  19.     </script>
  20.    
  21.     <script type="text/javascript">
  22.          function mostrarNumero(elem){
  23.             var texto = toWords(elem.value);
  24.             document.getElementById('mostrarAca').innerHTML = texto;
  25.          }
  26.     </script>
  27. </head>
  28.     <input type="text" onkeyup="mostrarNumero(this)" />
  29.     <p id="mostrarAca"></p>
  30. </body>
  31. </html>

El script que puse primero es mejor si lo ponés en un archivo externo, dejándo el texto comentado. No quites las fuentes ni el autor!
__________________
nahueljose.com.ar