SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculating Several Field Values

    Here's the snippet. The script stops (errors) at the first commented line and I can't figure out why. It worked in a shortened version wherein I had onely three values to calculate.

    <script language="javascript">

    function dp(print)
    {
    string = "" + print;
    number = string.length - string.indexOf('.');
    if (string.indexOf('.') == -1)
    return string + '.00';
    if (number == 1)
    return string + '00';
    if (number == 2)
    return string + '0';
    if (number > 3)
    return string.substring(0,string.length-number+3);
    return string;
    }

    function calculate()
    {
    document.form1.gross_percent.value = dp((document.form1.gross_sales.value)/(document.form1.gross_sales.value))
    document.form1.food_sales_percent.value = dp((document.form1.food_sales.value)/(document.form1.gross_sales.value))
    document.form1.liquor_sales_percent.value = dp((document.form1.liquor_sales.value)/(document.form1.gross_sales.value))
    document.form1.wine_sales_percent.value = dp((document.form1.wine_sales.value)/(document.form1.gross_sales.value))
    document.form1.beer_sales_percent.value = dp((document.form1.beer_sales.value)/(document.form1.gross_sales.value))
    document.form1.retail_percent.value = dp((document.form1.retail_sales.value)/(document.form1.gross_sales.value))
    document.form1.video_pool_percent.value = dp((document.form1.video_pool_sales.value)/(document.form1.gross_sales.value))
    document.form1.guest_appeaser_percent.value = dp((document.form1.guest_appeasers.value)/(document.form1.gross_sales.value))
    document.form1.dollar_off_percent.value = dp((document.form1.dollar_off_check.value)/(document.form1.gross_sales.value))
    document.form1.percent_check_percent.value = dp((document.form1.percent_off_check.value)/(document.form1.gross_sales.value))
    document.form1.misc_discounts_percent.value = dp((document.form1.misc_discounts.value)/(document.form1.gross_sales.value))
    document.form1.employee_discount_percent.value = dp((document.form1.employee_discounts.value)/(document.form1.gross_sales.value))
    document.form1.corporate_discounts_percent.value = dp((document.form1.corporate_discounts.value)/(document.form1.gross_sales.value))
    document.form1.manager_discount_percent.value = dp((document.form1.manager_discounts.value)/(document.form1.gross_sales.value))
    <!--document.form1.total_discounts.value = dp(eval(document.form1.guest_appeasers.value) + eval(document.form1.percent_off_check.value) + eval(document.form1.dollar_off_check.value)) + eval(document.form1.misc_discounts.value) + eval(document.form1.corporate_discounts.value) + eval(document.form1.manager_discounts.value) + eval(document.form1.employee_discounts.value))
    <!--document.form1.total_discounts_percent.value = dp((document.form1.total_discounts.value)/(document.form1.gross_sales.value))
    <!--document.form1.adj_gross_sales = eval(document.form1.gross_sales.value) - eval(document.form1.total_discounts.value))
    <!--document.form1.royalty.value = dp((document.form1.adj_gross_sales.value)*.065)
    <!--document.form1.namf.value = dp((document.form1.adj_gross_sales.value)*.01)
    <!--document.form1.total_royalty.value = dp((document.form1.adj_gross_sales.value)*.07)
    }

    </script>

    Mike (Mulligan2007)

  2. #2
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mike my man, you're a mess!

    1. In conditionals only the braces are allowed omission when there's one and only one statement preceding it e.g var n=1; if(n==1)n=2; n=2-1;else n=3;//will result in an error. However, if(n==1){n=2; n=2-1} else{n=3}//is allowed.

    2. It's best not to use print as a variable, as it is part of the native function print() that prints the page with an actual printer Simply choose something different.

    3. A good analogy I like is eval stands for evil. I don't advise using this method do to poor efficiency (slow).

    4. Use the toFixed function to get a certain decimal place e.g var n=96.4657; n=n.toFixed(2)//result 96.47

    5. To turn a string into a number use parseInt("n")for integers e.g n is 1,2,3... and/or parseFloat() for decimals; I assume this is the one you want.

    6. Use shorter variables, and if you plan on referencing a rather long object you should put it into a less wordy variable e.g var gs=document.form1.gross_percent; gp.value=functioncall()

    7. Clean your code and ifyou still have problems, come back and post your full code instead of a snippet; it's usually a lot easier to work with.

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I've always been a bit of a mess. It's a creative bloaks thing you know. You're quite right, I do need to clean this thing up a bit and will do so now that it is working. I went to the parseFloat function for the addition of the seven fields and that works fine. The bugs in the script above however were quite simple (typos actually) and I found them myself. A extra bracket in that long addition statement and the word "value" left out left of an object. The script now work quites well. Your suggestion re the tFixed function is useful because all of those percent calcs should show up as whole numbers plus two places decimal.
    I'm no JS programmer yet and probably never will be, but it helps to know alittle when I have to get certain things done in my webdesign business.

    Thanks again for your help
    Mike

  4. #4
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad I could help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •