1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JavaScript Help Needed

Discussion in 'JavaScript' started by designersshack, Jan 19, 2010.

  1. #1
    Hello Friends
    I need help on javascript.Let I am using this code below on my site-

    <script type="text/javascript">
    var a=28/13;
    	document.write(a);
    </script>
    Code (markup):
    The result printed on page is 2.1538461538461537

    But I want to print only two digits after point so the desired result should be 2.15
    How can I do it?
     
    designersshack, Jan 19, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try it

    
    <script type="text/javascript">
    function short_number(number){
      number = ""+number;
        point = number.indexOf(".");
        if(point!=-1){
            fpart = number.substring(0,point);
            spart = number.substring(point+1);
            if(spart.length>2){
                spart = spart.substring(0,2);
            }
            number = fpart+"."+spart;
        }else{
            number = number+".00";
        }
    
        return number;
    }
    
    var a=28/13;
    	document.write(short_number(a));
    </script>
    
    Code (markup):
     
    s_ruben, Jan 19, 2010 IP
  3. designersshack

    designersshack Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Its working well.Thank a lot.
     
    designersshack, Jan 19, 2010 IP