Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Mostrar decimales

Estas en el tema de Mostrar decimales en el foro de Frameworks JS en Foros del Web. Hola, estoy creando una pagina simple de pedido de productos con calculo del total en la misma pagina. El caso es que en los totales ...
  #1 (permalink)  
Antiguo 05/08/2010, 01:05
 
Fecha de Ingreso: agosto-2010
Mensajes: 3
Antigüedad: 13 años, 8 meses
Puntos: 0
Mostrar decimales

Hola, estoy creando una pagina simple de pedido de productos con calculo del total en la misma pagina. El caso es que en los totales de cada fila, si aparece el importe con decimales, pero en el subtotal y total no. Creo que el error está en este archivo order.js:


// UTILITY FUNCTIONS

function IsNumeric(n) {
return !isNaN(n);
}

function CleanNumber(value) {

// Assumes string input, removes all commas, dollar signs, and spaces
newValue = value.replace(",","");
newValue = newValue.replace("$","");
newValue = newValue.replace(/ /g,'');
return newValue;

}

function CommaFormatted(amount) {

var delimiter = ",";
var i = parseInt(amount);

if(isNaN(i)) { return ''; }

i = Math.abs(i);

var minus = '';
if (i < 0) { minus = '-'; }

var n = new String(i);
var a = [];

while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}

if (n.length > 0) { a.unshift(n); }

n = a.join(delimiter);

amount = "$" + minus + n;

return amount;

}


// ORDER FORM UTILITY FUNCTIONS

function applyName(klass, numPallets) {

var toAdd = $("td." + klass).text();

var actualClass = $("td." + klass).attr("rel");

$("input." + actualClass).attr("value", numPallets + " pallets");

}

function removeName(klass) {

var actualClass = $("td." + klass).attr("rel");

$("input." + actualClass).attr("value", "");

}

function calcTotalPallets() {

var totalPallets = 0;

$(".num-pallets-input").each(function() {

var thisValue = parseInt($(this).val());

if ( (IsNumeric(thisValue)) && (thisValue != '') ) {
totalPallets += parseInt(thisValue);
};

});

$("#total-pallets-input").val(totalPallets);

}

function calcProdSubTotal() {

var prodSubTotal = 0;

$(".row-total-input").each(function() {

var valString = $(this).val() || 0;

prodSubTotal += parseInt(valString);

});

$("#product-subtotal").val(CommaFormatted(prodSubTotal));

}

function calcShippingTotal() {

var totalPallets = $("#total-pallets-input").val() || 0;
var shippingRate = $("#shipping-rate").text() || 0;
var shippingTotal = totalPallets * shippingRate;

$("#shipping-subtotal").val(CommaFormatted(shippingTotal));

}

function calcOrderTotal() {

var orderTotal = 0;

var productSubtotal = $("#product-subtotal").val() || 0;
var shippingSubtotal = $("#shipping-subtotal").val() || 0;
var underTotal = $("#under-box").val() || 0;

var orderTotal = parseInt(CleanNumber(productSubtotal)) + parseInt(CleanNumber(shippingSubtotal));

$("#order-total").val(CommaFormatted(orderTotal));

$("#fc-price").attr("value", orderTotal);

}

// DOM READY
$(function() {

var inc = 1;

$(".product-title").each(function() {

$(this).addClass("prod-" + inc).attr("rel", "prod-" + inc);

var prodTitle = $(this).text();

$("#foxycart-order-form").append("<input type='hidden' name='" + prodTitle + "' value='' class='prod-" + inc + "' />");

inc++;

});

// Reset form on page load, optional
$("#order-table input[type=text]:not('#product-subtotal')").val("");
$("#product-subtotal").val("$0");
$("#shipping-subtotal").val("$0");
$("#fc-price").val("$0");
$("#order-total").val("$0");
$("#total-pallets-input").val("0");

// "The Math" is performed pretty much whenever anything happens in the quanity inputs
$('.num-pallets-input').bind("focus blur change keyup", function(){

// Caching the selector for efficiency
var $el = $(this);

// Grab the new quantity the user entered
var numPallets = CleanNumber($el.val());

// Find the pricing
var multiplier = $el
.parent().parent()
.find("td.price-per-pallet span")
.text();

// If the quantity is empty, reset everything back to empty
if ( (numPallets == '') || (numPallets == 0) ) {

$el
.removeClass("warning")
.parent().parent()
.find("td.row-total input")
.val("");

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

removeName(titleClass);

// If the quantity is valid, calculate the row total
} else if ( (IsNumeric(numPallets)) && (numPallets != '') ) {

var rowTotal = numPallets * multiplier;

$el
.removeClass("warning")
.parent().parent()
.find("td.row-total input")
.val(rowTotal);

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

applyName(titleClass, numPallets);

// If the quantity is invalid, let the user know with UI change
} else {

$el
.addClass("warning")
.parent().parent()
.find("td.row-total input")
.val("");

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

removeName(titleClass);

};

// Calcuate the overal totals
calcProdSubTotal();
calcTotalPallets();
calcShippingTotal();
calcOrderTotal();

});

});

Alguna idea? Gracias por la ayuda,

Enrique
  #2 (permalink)  
Antiguo 05/08/2010, 01:30
Avatar de caricatos
Moderador
 
Fecha de Ingreso: abril-2002
Ubicación: Torremolinos (Málaga)
Mensajes: 19.607
Antigüedad: 22 años
Puntos: 1284
Tema movido desde javascript

Evidentemente estás usando alguna librería, y sin saber cual, habrá que adivinarla...

Saludos
__________________
Por favor:
No hagan preguntas de temas de foros en mensajes privados... no las respondo
  #3 (permalink)  
Antiguo 05/08/2010, 02:27
 
Fecha de Ingreso: agosto-2010
Mensajes: 3
Antigüedad: 13 años, 8 meses
Puntos: 0
Respuesta: Mostrar decimales

no interesa

Última edición por greyreport; 05/08/2010 a las 15:56 Razón: ya no hace falta la direccion
  #4 (permalink)  
Antiguo 05/08/2010, 15:55
 
Fecha de Ingreso: agosto-2010
Mensajes: 3
Antigüedad: 13 años, 8 meses
Puntos: 0
Respuesta: Mostrar decimales

Solucioné el problema:

// UTILITY FUNCTIONS

function IsNumeric(n) {
return !isNaN(n);
}

function CleanNumber(value) {

// Assumes string input, removes all commas, dollar signs, and spaces
newValue = value.replace(",","");
newValue = newValue.replace("$","");
newValue = newValue.replace(/ /g,'');
return newValue;

}

function CommaFormatted(amount) {

var delimiter = ",";
var i = parseInt(amount);

if(isNaN(i)) { return ''; }

i = Math.abs(i);

var minus = '';
if (i < 0) { minus = '-'; }

var n = new String(i);
var a = [];

while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}

if (n.length > 0) { a.unshift(n); }

n = a.join(delimiter);

amount = "$" + minus + n;

return amount;

}


/esto esta mal, debería ser:
function CurrencyFormatted(amount) {
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
amount = minus + s;
return amount;
}
/


// ORDER FORM UTILITY FUNCTIONS

function applyName(klass, numPallets) {

var toAdd = $("td." + klass).text();

var actualClass = $("td." + klass).attr("rel");

$("input." + actualClass).attr("value", numPallets + " pallets");

}

function removeName(klass) {

var actualClass = $("td." + klass).attr("rel");

$("input." + actualClass).attr("value", "");

}

function calcTotalPallets() {

var totalPallets = 0;

$(".num-pallets-input").each(function() {

var thisValue = parseInt($(this).val());

if ( (IsNumeric(thisValue)) && (thisValue != '') ) {
totalPallets += parseInt(thisValue);
};

});

$("#total-pallets-input").val(totalPallets);

}

function calcProdSubTotal() {

var prodSubTotal = 0;

$(".row-total-input").each(function() {

var valString = $(this).val() || 0;

prodSubTotal += parseInt(valString);/esto esta mal, debería ser: parseFloat/

});

$("#product-subtotal").val(CommaFormatted(prodSubTotal));

}

function calcShippingTotal() {

var totalPallets = $("#total-pallets-input").val() || 0;
var shippingRate = $("#shipping-rate").text() || 0;
var shippingTotal = totalPallets * shippingRate;

$("#shipping-subtotal").val(CommaFormatted(shippingTotal));

}

function calcOrderTotal() {

var orderTotal = 0;

var productSubtotal = $("#product-subtotal").val() || 0;
var shippingSubtotal = $("#shipping-subtotal").val() || 0;
var underTotal = $("#under-box").val() || 0;

var orderTotal = parseInt(CleanNumber(productSubtotal)) + parseInt(CleanNumber(shippingSubtotal)); /esto esta mal, debería ser:parseFloat/

$("#order-total").val(CommaFormatted(orderTotal));

$("#fc-price").attr("value", orderTotal);

}

// DOM READY
$(function() {

var inc = 1;

$(".product-title").each(function() {

$(this).addClass("prod-" + inc).attr("rel", "prod-" + inc);

var prodTitle = $(this).text();

$("#foxycart-order-form").append("<input type='hidden' name='" + prodTitle + "' value='' class='prod-" + inc + "' />");

inc++;

});

// Reset form on page load, optional
$("#order-table input[type=text]:not('#product-subtotal')").val("");
$("#product-subtotal").val("$0.00");
$("#shipping-subtotal").val("$0");
$("#fc-price").val("$0");
$("#order-total").val("$0");
$("#total-pallets-input").val("0");

// "The Math" is performed pretty much whenever anything happens in the quanity inputs
$('.num-pallets-input').bind("focus blur change keyup", function(){

// Caching the selector for efficiency
var $el = $(this);

// Grab the new quantity the user entered
var numPallets = CleanNumber($el.val());

// Find the pricing
var multiplier = $el
.parent().parent()
.find("td.price-per-pallet span")
.text();

// If the quantity is empty, reset everything back to empty
if ( (numPallets == '') || (numPallets == 0) ) {

$el
.removeClass("warning")
.parent().parent()
.find("td.row-total input")
.val("");

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

removeName(titleClass);

// If the quantity is valid, calculate the row total
} else if ( (IsNumeric(numPallets)) && (numPallets != '') ) {

var rowTotal = numPallets * multiplier;

$el
.removeClass("warning")
.parent().parent()
.find("td.row-total input")
.val(rowTotal);

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

applyName(titleClass, numPallets);

// If the quantity is invalid, let the user know with UI change
} else {

$el
.addClass("warning")
.parent().parent()
.find("td.row-total input")
.val("");

var titleClass = $el.parent().parent().find("td.product-title").attr("rel");

removeName(titleClass);

};

// Calcuate the overal totals
calcProdSubTotal();
calcTotalPallets();
calcShippingTotal();
calcOrderTotal();

});
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 17:23.