Calculating decimals

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    Calculating decimals

    Hi guys,

    I've been trying to write script that counts decimals. I know Javascript won't work with them so you have to bypass it.

    I've looked around on the net and it seems that most people are using the match.pow() method. However I don't understand why? But then again I was never good at Math. Considering I flunked every year :-)

    The only thing I want to do is add. So if I have two times 2.48 I would like it to show 4.96. Now it just gives me 4.

    If anyone could clear it up for me, that would be nice.
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    If your numbers are coming from user input, make sure they are numbers:
    number=parseFlo at(string) or Number(string)

    You can return the number as a string formatted to the number of decimals you want by returning (number).toFixe d(2) or (number).toFixe d(3)

    You can force 3 decimal accuracy by multiplying your number by 1000 and dividing the integer part by the same.

    Math.floor(1.23 5666*1000)/1000 returns the number 1.235
    (1.235666).toFi xed(3) returns the string '1.235'

    Math.pow(10,n) return 10 to the nth power.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      Originally posted by Cainnech
      ... I know Javascript won't work with them so you have to bypass it. ...
      mrhoo already gave you a very good answer ... and as you should see ... JavaScript WORKS with them and you DON'T need to bypass anything. like every programming language JavaScript has the common primitive data types like strings, booleans and even numbers that could be integers or floats :)
      i think what you really mean is that every user input that comes from a webpage's form is first considered a string and you should type-cast it to use it as a number. some operations do implicit type-casts but the better way would be to cast explicitly before any operation so that you could know what data you have to process. while javascript don't declare types of variables it is definitly important what type a concrete instance of a variable is ... so you couldn't multiply 2 strings but you could multiply 2 numbers ;)

      kind regards

      Comment

      • Cainnech
        New Member
        • Nov 2007
        • 132

        #4
        Thanks guys,

        You cleared things up for me.
        I now also know why my script wouldn't work. I was using parseInt, but ofcourse silly me forgot that this is only for whole numbers and not for decimals, so thank you Magoo for pointing out Number() to me.

        And a second problem I had was that my decimals were divided by a "," and not by a "." which resulted in NaN. It took me a while to figure that out but in the end everything works !
        :-)

        Greets,

        Cainnech

        Comment

        Working...