SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax for mathematical formula

    Hi

    I have a range of values. Ive written the following formula. It does not seem to work. Can someone please tell me if ive got the operators and syntax right??

    aMin_term_cost.value = ((aMonthly_Cost.value * aContract_Period.value) + aHandset_Cost.value ) - ((aMonthly_Cost.value * aMonths_Free.value) + ( aMonthly_Cost.value * (aMonths_Half_Price.value/2)) + aDiscount.value);
    thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why doesn't it work? Do you get undefined?
    If so, maybe some of the references don't work because they don't exist and you'll get an error on fetching it's [code].value[/code property.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    all the values exist but when you enter aDiscount.value or aHandset_Cost.value the wrong answer appears.

    is it something to do with the + operator

    if the answer is 120 when i enter a value for handset_cost ( eg 12)
    the answer i get is 12012 . i.e the 12 is added on but not mathematically

    if you don't enter the 2 values above the formula seems to work

  4. #4
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yass2006
    ...is it something to do with the + operator

    if the answer is 120 when i enter a value for handset_cost ( eg 12)
    the answer i get is 12012 . i.e the 12 is added on but not mathematically
    Yes, I think it's to do with the addition operator. Those form.field.values that you are working with are still strings. The + operator will concatenate them as strings.
    You need to change the form fields from strings to numbers before you perform your calculations.
    I think you can make a numeric string into a number by subtracting zero from it.

    "120" + 12 will equal 12012
    ("120" - 0) + 12 will equal 132

  5. #5
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the parseFloat() or parseInt() functions to convert a string to a float or integer.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Also Number() will convert strings to numbers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •