0

I've searched for and reviewed other posts on this site related to this question, but being someone in the very early stages of trying to learn javascript, I don't know how to apply what I'm seeing.

I need the result calculations located here ( http://jsfiddle.net/hughett/xwnbepzr/ ) to be formatted with decimals and commas. In other words, the results below should be formatted like the following:

Watts saved per lamp 53
Total watts saved 5,300 (instead of 5300)
Total kilowatts saved 5
KWh saved per year 22,260 (instead of 22260)
Cost savings per year $3,339.00 (instead of 3339)

$(function(){
var tws = 0;
var tks = 0;
var kspy = 0;
var wspl = 0;
var cspy = 0;
var sp = 0;
var ror = 0;
$("input[name=old_watts], input[name=new_watts]").blur(function(e){
    calc_wspl();
});
$("input[name=n]").blur(function(e){
    calc_tws();
});
$("input[name=hours], input[name=days], input[name=weeks]").blur(function(e){
    calc_kspy();
});
$("input[name=cost]").blur(function(e){
    calc_cspy();
});
$("input[name=upgrade]").blur(function(e){
    calc_summary();
})

function calc_wspl(){
    wspl= parseFloat($("input[name=old_watts]").val() ) - parseFloat( $("input[name=new_watts]").val() );
    $(".wspl").html(wspl > 0 ? wspl : '');
    calc_tws();
}
function calc_tws() {
    tws = wspl * parseFloat( $("input[name=n]").val() );
    $(".tws").html(tws > 0 ? tws : '');
    $(".tks").html(tws > 0 ? parseInt(tws / 1000) : '');
    calc_kspy();
}
function calc_kspy() {
    if (tws > 0)
    {
        kspy = parseFloat($("input[name=hours]").val() ) * parseFloat($("input[name=days]").val() ) * parseFloat($("input[name=weeks]").val() ) * tws / 1000;
        $(".kspy").html(kspy > 0 ? kspy : '');
        calc_cspy();
    }
}
function calc_cspy(){
    cspy = parseFloat($("input[name=cost]").val() ) * kspy;
    $(".cspy").html(cspy >= 1 ? "$" + parseInt(cspy) : '');
    calc_summary();
}
function calc_summary(){
    if (cspy) {
        sp = parseFloat($("input[name=upgrade]").val()) / cspy;
        if (sp) {
            sp = (sp < 100) ? sp.toString().substring(0, 4) : sp;
            $(".sp").html(sp + " year" + ((sp > 1) ? 's': ''));
            $(".ror").html(parseInt((1/sp) * 100) + '%');
        }
    }
}
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Hughett
  • 3
  • 2
  • possible duplicate of [How can I format numbers as money in JavaScript?](http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript) – jcubic Mar 30 '15 at 13:16
  • http://stackoverflow.com/a/7266497/4028085 – brso05 Mar 30 '15 at 13:16

0 Answers0