/    Sign up×
Community /Pin to ProfileBookmark

Calling decimalFormat

I am new to JavaScript and truthfully it is confusing for me so please bear with me. ?

I will start by telling you what I am trying to do.

First, I have [B]16 [/B]text fields [B]Qty1[/B], [B]Qty2[/B], [B]Qty3[/B], [B]Qty4[/B], [B]Price1[/B], [B]Price2[/B], [B]Price3[/B], [B]Price4[/B], [B]Subtotal1[/B], [B]Subtotal2[/B], [B]Subtotal3[/B], [B]Subtotal4[/B], and of course I have a [B]Grand Total[/B]. All I am trying to do is *(times) [B]QTY[/B] by the [B]Price[/B] for the [B]Subtotal[/B]. I want the [B]Subtotal[/B] textbox to display in a decimal format and the [B]Grand Total[/B] to display in a Dollar Format.

I can get it to return the Subtotal amount as a regular number, and I have also managed to get the Dollar Format to work for the Grand Total, but I can not figure out how to get the decimal point to work for the Subtotal.

Pretty simple Huh? Not for me. It am confused and pulling my hair out. Any help is appreciated, Script to get this to work would be great. Tutorials to learn more about Javascript. Especially tutorials for simple minded people like myself.

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@toicontienApr 18.2008 — Can you post the JavaScript you are using? We can't help unless we see your code.
Copy linkTweet thisAlerts:
@JMRKERApr 18.2008 — If I'm in the right function, try this:
[code=php]
result.value = (QTY_UGMP * UGMP_PRICE).toFixed(2);
[/code]
Copy linkTweet thisAlerts:
@twhittauthorApr 19.2008 — Thanks for responding JMRKER,

But sorry to tell you it didn't work. Thanks for trying.
Copy linkTweet thisAlerts:
@JMRKERApr 19.2008 — What didn't work? What kind of errors are you getting?

Where exactly in the code do you calculate the 'subtotal' decimal to display?

What exactly did change ... anything?

Try putting the .toFixed(2) function where you do the calculations.

Or define your problem with more specific information.
Copy linkTweet thisAlerts:
@twhittauthorApr 19.2008 — What I meant by it didn't work. Is it didn't do anything. Now I am sure your probably right about the toFixed method I am not saying that it is not the proper way. It just didn't work in my case (Probably something I did wrong). At the bottom it said error on page, and please don't ask me what the error was because I don't no. I don't have a debugging program at home.


I attached the cost I am using. It is a big mess and I am sure there is a better way to do this but I don't no how. What I am trying to do is in the txtTOTAL_AGMC (Subtotal of QTY Textbox). I would like for it to display something like this:

The customer purchases 1 (QTY) book times 40.00 (Price) and it should read (40.00) in the txtTOTAL_AGMC textbox without the Dollar sign.

I hope this makes since.


I tried adding the dollarformat function to the code right before the torder function code started. After minipulating the code some by taking out the dollar sign I got it to return the subtotal like I was wanting it, but it wasn'y totaling anything it only retuned 0.00. When I call the addition function it does the dollarformat and when I call the calculate function it totals it but with the wrong format, numbers only no decimals.

I don't no what I am doing is it possible to call two funtions? Or am I doing this whole thing wrong.

SORRY FOR BEING SO LONG WINDED.




<SCRIPT LANGUAGE="JAVASCRIPT">

<!--Hide from old browsers


function calculateAGMC() {

QTY_AGMC = document.frm_GMOF.txtQTY_AGMC.value

AGMC_PRICE = document.frm_GMOF.txtAGMC_PRICE.value

if(Number(QTY_AGMC) && Number(AGMC_PRICE)) {

QTY_AGMC = Number(QTY_AGMC)

AGMC_PRICE = Number(AGMC_PRICE)

result = document.frm_GMOF.txtTOTAL_AGMC

result.value = QTY_AGMC * AGMC_PRICE;

}


else {

alert("Please enter the quantity of 2006 General Minutes Clothbound books you wish to purchase before pressing the Total Qty button.")

}

}

function calculateAGMP() {
QTY_AGMP = document.frm_GMOF.txtQTY_AGMP.value

AGMP_PRICE = document.frm_GMOF.txtAGMP_PRICE.value

if(Number(QTY_AGMP) && Number(AGMP_PRICE)) {

QTY_AGMP = Number(QTY_AGMP)

AGMP_PRICE = Number(AGMP_PRICE)

result = document.frm_GMOF.txtTOTAL_AGMP

result.value = QTY_AGMP * AGMP_PRICE;

}


else {

alert("Please enter the quantity of 2006 General Minutes Paperbound books you wish to purchase before pressing the Total Qty button.")

}

}

function calculateUGMC() {

QTY_UGMC = document.frm_GMOF.txtQTY_UGMC.value

UGMC_PRICE = document.frm_GMOF.txtUGMC_PRICE.value

if(Number(QTY_UGMC) && Number(UGMC_PRICE)) {

QTY_UGMC = Number(QTY_UGMC)

UGMC_PRICE = Number(UGMC_PRICE)

result = document.frm_GMOF.txtTOTAL_UGMC

result.value = QTY_UGMC * UGMC_PRICE;

}


else {

alert("Please enter the quantity of 2006 General Minutes Clothbound books you wish to purchase before pressing the Total Qty button.")

}

}

function calculateUGMP() {

QTY_UGMP = document.frm_GMOF.txtQTY_UGMP.value

UGMP_PRICE = document.frm_GMOF.txtUGMP_PRICE.value

if(Number(QTY_UGMP) && Number(UGMP_PRICE)) {

QTY_UGMP = Number(QTY_UGMP)

UGMP_PRICE = Number(UGMP_PRICE)

result = document.frm_GMOF.txtTOTAL_UGMP

result.value = QTY_UGMP * UGMP_PRICE;

}


else {

alert("Please enter the quantity of 2006 General Minutes Paperbound books you wish to purchase before pressing the Total Qty button.")


}

}



//Totals Qtys for the books

function torder() {

//Add first input value

var totalAGMC = document.frm_GMOF.txtTOTAL_AGMC.value

var totalAGMC = parseFloat(totalAGMC,10)

totalAGMC =(isNaN(totalAGMC))?0:totalAGMC;

//Add second input value

var totalAGMP = document.frm_GMOF.txtTOTAL_AGMP.value

var totalAGMP = parseFloat(totalAGMP,10)

totalAGMP=(isNaN(totalAGMP))?0:totalAGMP;

//Add third input value

var totalUGMC = document.frm_GMOF.txtTOTAL_UGMC.value

var totalUGMC = parseFloat(totalUGMC,10)

totalUGMC=(isNaN(totalUGMC))?0:totalUGMC;

//Add fourth input value

var totalUGMP = document.frm_GMOF.txtTOTAL_UGMP.value

var totalUGMP = parseFloat(totalUGMP,10)

totalUGMP=(isNaN(totalUGMP))?0:totalUGMP;

//Add fifth input value

var totalShipping = document.frm_GMOF.txtSHIPPING.value

var totalShipping =parseFloat(totalShipping,10)

totalShipping=(isNaN(totalShipping))?0:totalShipping;



//Add them together

return (totalAGMC) + (totalAGMP) +(totalUGMC) + (totalUGMP) + (totalShipping);

}







//FUNCTION WILL PARSE THE TOTAL AS DOLLAR FORMAT

function dollarformat(num) {

num = num.toString().replace(/$|,/g,'');

if(isNaN(num)) 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);

}

//CALCUATES TOTAL AMOUNT

function calculate() {

var x = torder()


max = x

document.frm_GMOF.Payment.value = dollarformat(max)

}

function addition() {

document.frm_GMOF.Payment.value = torder()

document.frm_GMOF.Payment.value = dollarformat(document.frm_GMOF.Payment.value)

calculate()

}



//-->

</Script>
Copy linkTweet thisAlerts:
@twhittauthorApr 19.2008 — Ignore my last post. I no it is confusing.

I ran a test and I have 2 different sets of code to show you what I am talking about.


I have to change the function I'm calling each time.

  • 1. When I call the [B]addition function[/B] it shows 0.00 like I want it too but without mutipling like I need for it to do.


  • <i>
    </i>&lt;HTML&gt;
    &lt;HEAD&gt;

    &lt;SCRIPT Language ="JavaScript"&gt;
    &lt;!-- Hide from old browsers

    <i> </i> function calculate() {
    QTY_AGMC = document.frm_GMOF.txtQTY_AGMC.value
    AGMC_PRICE = document.frm_GMOF.txtAGMC_PRICE.value

    if(Number(QTY_AGMC) &amp;&amp; Number(AGMC_PRICE)) {
    QTY_AGMC = Number(QTY_AGMC)
    AGMC_PRICE = Number(AGMC_PRICE)
    result = document.frm_GMOF.TOTAL_AGMC

    result.value = QTY_AGMC * AGMC_PRICE
    }


    else {
    alert("Please enter numbers in the boxes")
    }
    }



    //FUNCTION WILL PARSE THE TOTAL AS DOLLAR FORMAT
    function dollarformat(num) {

    <i> </i>if(isNaN(num)) num = "0";
    <i> </i> cents = Math.floor((num*100+0.5)%100);
    <i> </i> num = Math.floor((num*100+0.5)/100).toString();
    <i> </i>if(cents &lt; 10) cents = "0" + cents;
    <i> </i> for (var i = 0; i &lt; Math.floor((num.length-(1+i))/3); i++)
    <i> </i> num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    <i> </i>return (num + '.' + cents);
    }
    //CALCUATES TOTAL AMOUNT
    function QTY_BOOKS() {
    var x = calculate() <br/>
    max = x
    document.frm_GMOF.TOTAL_AGMC.value = dollarformat(max)
    }

    function addition() {
    document.frm_GMOF.TOTAL_AGMC.value = calculate()
    document.frm_GMOF.TOTAL_AGMC.value = dollarformat(document.frm_GMOF.TOTAL_AGMC.value)
    QTY_BOOKS()
    }




    //-&gt;
    &lt;/SCRIPT&gt;





    &lt;TITLE&gt;Multiply&lt;/TITLE&gt;
    &lt;/HEAD&gt;
    &lt;BODY&gt;
    &lt;CENTER&gt;&lt;FORM NAME = frm_GMOF&gt;

    QTY of AGMC: &lt;INPUT TYPE = Text NAME = txtQTY_AGMC SIZE = 5 value =""&gt;


    Cost: &lt;INPUT TYPE = Text NAME = txtAGMC_PRICE SIZE = 5 value = "40.00"&gt;
    &lt;P&gt;
    &lt;INPUT TYPE = Button VALUE = "Times Table" onClick = addition()&gt;
    &lt;P&gt;
    &lt;Input Type = Reset value = " Reset "&gt;
    &lt;P&gt;
    &lt;input name="TOTAL_AGMC" type="text" id="TOTAL_AGMC" size="15" maxlength="15" /&gt;

    &lt;/FORM&gt;&lt;/CENTER&gt;


    &lt;/BODY&gt;
    &lt;/HTML&gt;




    And this code calls the [B]calculate function[/B].

  • 2. This function adds it but without the decimal places.
    <i>
    </i>&lt;HTML&gt;
    &lt;HEAD&gt;

    &lt;SCRIPT Language ="JavaScript"&gt;
    &lt;!-- Hide from old browsers

    <i> </i> function calculate() {
    QTY_AGMC = document.frm_GMOF.txtQTY_AGMC.value
    AGMC_PRICE = document.frm_GMOF.txtAGMC_PRICE.value

    if(Number(QTY_AGMC) &amp;&amp; Number(AGMC_PRICE)) {
    QTY_AGMC = Number(QTY_AGMC)
    AGMC_PRICE = Number(AGMC_PRICE)
    result = document.frm_GMOF.TOTAL_AGMC

    result.value = QTY_AGMC * AGMC_PRICE
    }


    else {
    alert("Please enter numbers in the boxes")
    }
    }



    //FUNCTION WILL PARSE THE TOTAL AS DOLLAR FORMAT
    function dollarformat(num) {

    <i> </i>if(isNaN(num)) num = "0";
    <i> </i> cents = Math.floor((num*100+0.5)%100);
    <i> </i> num = Math.floor((num*100+0.5)/100).toString();
    <i> </i>if(cents &lt; 10) cents = "0" + cents;
    <i> </i> for (var i = 0; i &lt; Math.floor((num.length-(1+i))/3); i++)
    <i> </i> num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    <i> </i>return (num + '.' + cents);
    }
    //CALCUATES TOTAL AMOUNT
    function QTY_BOOKS() {
    var x = calculate() <br/>
    max = x
    document.frm_GMOF.TOTAL_AGMC.value = dollarformat(max)
    }

    function addition() {
    document.frm_GMOF.TOTAL_AGMC.value = calculate()
    document.frm_GMOF.TOTAL_AGMC.value = dollarformat(document.frm_GMOF.TOTAL_AGMC.value)
    QTY_BOOKS()
    }




    //-&gt;
    &lt;/SCRIPT&gt;





    &lt;TITLE&gt;Multiply&lt;/TITLE&gt;
    &lt;/HEAD&gt;
    &lt;BODY&gt;
    &lt;CENTER&gt;&lt;FORM NAME = frm_GMOF&gt;

    QTY of AGMC: &lt;INPUT TYPE = Text NAME = txtQTY_AGMC SIZE = 5 value =""&gt;


    Cost: &lt;INPUT TYPE = Text NAME = txtAGMC_PRICE SIZE = 5 value = "40.00"&gt;
    &lt;P&gt;
    &lt;INPUT TYPE = Button VALUE = "Times Table" onClick = calculate()&gt;
    &lt;P&gt;
    &lt;Input Type = Reset value = " Reset "&gt;
    &lt;P&gt;
    &lt;input name="TOTAL_AGMC" type="text" id="TOTAL_AGMC" size="15" maxlength="15" /&gt;

    &lt;/FORM&gt;&lt;/CENTER&gt;


    &lt;/BODY&gt;
    &lt;/HTML&gt;


    I hope this makes more since. Please help!
  • Copy linkTweet thisAlerts:
    @twhittauthorApr 22.2008 — Sorry JRCKER, my bad. I feel like an idiot. I guess I staired at this one to long. You were exactly right the whole time. All I needed to do was to walk away from it for awhile and realize the reason it wasn't working is because I had to change it in all my functions. ?
    Copy linkTweet thisAlerts:
    @JMRKERApr 22.2008 — SoooOK, I've done similar things. :o

    Glad it worked out well though.

    Good Luck! ?
    ×

    Success!

    Help @twhitt spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...