Foros del Web » Programando para Internet » Javascript »

Ayuda

Estas en el tema de Ayuda en el foro de Javascript en Foros del Web. Buenas me pueden ayudar a poner miles a esta tabla es decir en la tabla sale total 1000 quiero que salga asi 1.000 Aqui les ...
  #1 (permalink)  
Antiguo 25/05/2010, 22:34
 
Fecha de Ingreso: abril-2010
Mensajes: 6
Antigüedad: 14 años
Puntos: 0
Ayuda

Buenas me pueden ayudar a poner miles a esta tabla es decir
en la tabla sale total 1000 quiero que salga asi 1.000


Aqui les dejo la tabla pra ver si me pueden ayudar
Un saludo y gracias

Código Javascript:
Ver original
  1. <HTML>
  2. <HEAD>
  3.  
  4. <SCRIPT>
  5.  
  6. function dp(price)
  7. {
  8.    string = "" + price;
  9.    number = string.length - string.indexOf('.');
  10.    if (string.indexOf('.') == -1)
  11.       return string + '.00';
  12.    if (number == 1)
  13.       return string + '00';
  14.    if (number == 2)
  15.       return string + '0';
  16.    if (number > 3)
  17.       return string.substring(0,string.length-number+3);
  18. return string;
  19. }
  20.  
  21. function calculate()
  22. {
  23. document.calcform.total1.value = dp((document.calcform.price1.value)*(document.calcform.quantity1.value))
  24. document.calcform.total2.value = dp((document.calcform.price2.value)*(document.calcform.quantity2.value))
  25. document.calcform.total3.value = dp((document.calcform.price3.value)*(document.calcform.quantity3.value))
  26. document.calcform.total4.value = dp((document.calcform.price4.value)*(document.calcform.quantity4.value))
  27. document.calcform.total5.value = dp((document.calcform.price5.value)*(document.calcform.quantity5.value))
  28. document.calcform.total6.value = dp((document.calcform.price6.value)*(document.calcform.quantity6.value))
  29. document.calcform.total7.value = dp((document.calcform.price7.value)*(document.calcform.quantity7.value))
  30. document.calcform.total8.value = dp((document.calcform.price8.value)*(document.calcform.quantity8.value))
  31. document.calcform.total9.value = dp((document.calcform.price9.value)*(document.calcform.quantity9.value))
  32. document.calcform.total10.value = dp((document.calcform.price10.value)*(document.calcform.quantity10.value))
  33.  
  34. document.calcform.subtotal.value = dp(eval(document.calcform.total1.value) + eval(document.calcform.total2.value) + eval(document.calcform.total3.value) + eval(document.calcform.total4.value) + eval(document.calcform.total5.value) + eval(document.calcform.total6.value) + eval(document.calcform.total7.value) + eval(document.calcform.total8.value) + eval(document.calcform.total9.value) + eval(document.calcform.total10.value))
  35.  
  36. document.calcform.tax.value = dp((document.calcform.subtotal.value)*0.175)
  37. document.calcform.total.value = dp((document.calcform.subtotal.value)*1.175)
  38. }
  39.  
  40. </SCRIPT>
  41.  
  42.  
  43. </HEAD>
  44.  
  45. <BODY bgcolor="white" onload="calculate">
  46.  
  47.  
  48. <CENTER>
  49.  
  50. <FORM name="calcform" action="mailto:" tppabs="mailto:" method="post">
  51.  
  52. <TABLE cellpadding=2 border=2 cellspacing=2><TR><TD>QUANTITY</TD><TD>PRICE</TD><TD>TOTAL</TD></TR>
  53.  
  54. <TR>
  55. <TD><INPUT type="text" name="quantity1" ONCHANGE="calculate()"></TD>
  56. <TD><INPUT type="text" name="price1" ONCHANGE="calculate()"></TD>
  57. <TD><INPUT type="text" name="total1"></TD>
  58. </TR>
  59.  
  60. <TR>
  61. <TD><INPUT type="text" name="quantity2" ONCHANGE="calculate()"></TD>
  62. <TD><INPUT type="text" name="price2" ONCHANGE="calculate()"></TD>
  63. <TD><INPUT type="text" name="total2"></TD>
  64. </TR>
  65.  
  66. <TR>
  67. <TD><INPUT type="text" name="quantity3" ONCHANGE="calculate()"></TD>
  68. <TD><INPUT type="text" name="price3" ONCHANGE="calculate()"></TD>
  69. <TD><INPUT type="text" name="total3"></TD>
  70. </TR>
  71.  
  72. <TR>
  73. <TD><INPUT type="text" name="quantity4" ONCHANGE="calculate()"></TD>
  74. <TD><INPUT type="text" name="price4" ONCHANGE="calculate()"></TD>
  75. <TD><INPUT type="text" name="total4"></TD>
  76. </TR>
  77.  
  78.  
  79. <TR>
  80. <TD><INPUT type="text" name="quantity5" ONCHANGE="calculate()"></TD>
  81. <TD><INPUT type="text" name="price5" ONCHANGE="calculate()"></TD>
  82. <TD><INPUT type="text" name="total5"></TD>
  83. </TR>
  84.  
  85.  
  86. <TR>
  87. <TD><INPUT type="text" name="quantity6" ONCHANGE="calculate()"></TD>
  88. <TD><INPUT type="text" name="price6" ONCHANGE="calculate()"></TD>
  89. <TD><INPUT type="text" name="total6"></TD>
  90. </TR>
  91.  
  92.  
  93. <TR>
  94. <TD><INPUT type="text" name="quantity7" ONCHANGE="calculate()"></TD>
  95. <TD><INPUT type="text" name="price7" ONCHANGE="calculate()"></TD>
  96. <TD><INPUT type="text" name="total7"></TD>
  97. </TR>
  98.  
  99.  
  100. <TR>
  101. <TD><INPUT type="text" name="quantity8" ONCHANGE="calculate()"></TD>
  102. <TD><INPUT type="text" name="price8" ONCHANGE="calculate()"></TD>
  103. <TD><INPUT type="text" name="total8"></TD>
  104. </TR>
  105.  
  106.  
  107. <TR>
  108. <TD><INPUT type="text" name="quantity9" ONCHANGE="calculate()"></TD>
  109. <TD><INPUT type="text" name="price9" ONCHANGE="calculate()"></TD>
  110. <TD><INPUT type="text" name="total9"></TD>
  111. </TR>
  112.  
  113.  
  114. <TR>
  115. <TD><INPUT type="text" name="quantity10" ONCHANGE="calculate()"></TD>
  116. <TD><INPUT type="text" name="price10" ONCHANGE="calculate()"></TD>
  117. <TD><INPUT type="text" name="total10"></TD>
  118. </TR>
  119.  
  120.  
  121. <TR><TD></TD><TD>SUBTOTAL</TD><TD><INPUT type="text" name="subtotal"></TD></TR>
  122. <TR><TD></TD><TD>TAX - 17.5%</TD><TD><INPUT type="text" name="tax"></TD></TR>
  123. <TR><TD></TD><TD>GRAND TOTALL</TD><TD><INPUT type="text" name="total"></TD></TR>
  124. </TABLE><INPUT type="button" Value="PRESS FIRST to calculate the values" ONCLICK="calculate()"><BR><INPUT type="submit" Value="Then click this to SUBMIT"></form>
  125.  
  126.  
  127. </body>
  128. </html>
  #2 (permalink)  
Antiguo 26/05/2010, 05:39
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 1 mes
Puntos: 574
Respuesta: Ayuda

Google "Formatear numeros javascript" 891.000 documentos elige!!!

Quim

Última edición por quimfv; 26/05/2010 a las 06:05
  #3 (permalink)  
Antiguo 26/05/2010, 05:49
Avatar de zerokilled
Javascripter
 
Fecha de Ingreso: abril-2009
Ubicación: Isla del Encanto, La Borinqueña [+>==]
Mensajes: 8.050
Antigüedad: 15 años
Puntos: 1485
Respuesta: Ayuda

tambien puedes usar la funcion toLocaleString de los numeros el cual formatea el numero de acuerdo a la configuracion del sistema del usuario.
__________________
la maldad es una virtud humana,
y la espiritualidad es la lucha del hombre contra su maldad.
  #4 (permalink)  
Antiguo 26/05/2010, 15:30
 
Fecha de Ingreso: agosto-2008
Mensajes: 15
Antigüedad: 15 años, 8 meses
Puntos: 0
De acuerdo Respuesta: Ayuda

/**
* Convierte a formato con comas y puntos decimales
* @autor Daniel Del Angel
* @param num Numero sin comas, con o sin decimales
* Devuelve el numero formateado con comas y decimales
* Pero funciona solo con numeros > 0 si es negativo = 0
* ejemplo 12345 devuelve 12,345.00
*/
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if (isNaN(num)) num = "0";
if (num < 0) num = "0";
cents = Math.floor((num * 100 + 0.5) % 100);
num = Math.floor((num * 100 + 0.5) / 100).toString();
if(cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
return (num + '.' + cents);
}

Esta funcion hace lo que quieres pero con comas si deseas por fuerza el punto cambia la linea que tiene la coma por el punto y obvio el punto por la coma son las 2 ultimas líneas

obvio la invocacion seria en el window.onload

y lo harias con javascript si no sabes como me avisas y con gusto te explico




por

Última edición por Danny_fury00; 26/05/2010 a las 15:32 Razón: unirme al tema
  #5 (permalink)  
Antiguo 26/05/2010, 23:41
 
Fecha de Ingreso: abril-2010
Mensajes: 6
Antigüedad: 14 años
Puntos: 0
Respuesta: Ayuda

Podrías explicar
Lo intente pero no funciono
  #6 (permalink)  
Antiguo 27/05/2010, 08:33
 
Fecha de Ingreso: agosto-2008
Mensajes: 15
Antigüedad: 15 años, 8 meses
Puntos: 0
Respuesta: Ayuda

despues de hacer tus calculos

document.calcform.total.value = formatCurrency(document.calcform.total.value);

También note que no pones ";" al finalizar tus sentencias no se si eso te marque error pero es recomendable ponerlos.

para modificar la funcion y en lugar que te de 1,200.00 te de 1.200,00

deberas cambiarla la original es la que ya habia puesto

/**
* Convierte a formato con comas y puntos decimales
* @autor Daniel Del Angel
* @param num Numero sin comas, con o sin decimales
* Devuelve el numero formateado con comas y decimales
* Pero funciona solo con numeros > 0 si es negativo = 0
* ejemplo 12345 devuelve 12,345.00
*/
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if (isNaN(num)) num = "0";
if (num < 0) num = "0";
cents = Math.floor((num * 100 + 0.5) % 100);
num = Math.floor((num * 100 + 0.5) / 100).toString();
if(cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
return (num + '.' + cents);
}

y la modificada sería

/**
* Convierte a formato con comas y puntos decimales
* @autor Daniel Del Angel
* @param num Numero sin comas, con o sin decimales
* Devuelve el numero formateado con comas y decimales
* Pero funciona solo con numeros > 0 si es negativo = 0
* ejemplo 12345 devuelve 12,345.00
*/
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if (isNaN(num)) num = "0";
if (num < 0) num = "0";
cents = Math.floor((num * 100 + 0.5) % 100);
num = Math.floor((num * 100 + 0.5) / 100).toString();
if(cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + '.' +
num.substring(num.length - (4 * i + 3));
return (num + ',' + cents);
}

Última edición por Danny_fury00; 27/05/2010 a las 08:51

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:22.