parseInt .5

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    parseInt .5

    I've noticed that doing:

    var x=any number between -1 and 1, like 0.5

    parsInt(x)

    yields NaN.

    I was expecting zero. Is there another way of doing this?

    Jeff


  • Lasse Reichstein Nielsen

    #2
    Re: parseInt .5

    "Jeff Thies" <nospam@nospam. net> writes:
    [color=blue]
    > I've noticed that doing:
    >
    > var x=any number between -1 and 1, like 0.5
    >
    > parsInt(x)
    >
    > yields NaN.
    >
    > I was expecting zero.[/color]

    I am getting 0, both for "0.5" and 0.5 (the number). I'm using Opera 7,
    what browser are you using?

    Hmm, let me check ... yes. Netscape 4:
    parseInt(0.5)
    gives NaN. The reason is that Netscape 4's toString turns 0.5 into the
    string ".5", not "0.5" as the other browsers I checked.
    [color=blue]
    > Is there another way of doing this[/color]

    Don't use numbers as arguments to parseInt, it's meant for strings.

    What are you trying to do?
    If you want to turn a number into an integer, use Math.round, Math.floor
    or Math.ceil.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Jeff Thies

      #3
      Re: parseInt .5

      > > I've noticed that doing:[color=blue][color=green]
      > >
      > > var x=any number between -1 and 1, like 0.5
      > >
      > > parsInt(x)
      > >
      > > yields NaN.
      > >
      > > I was expecting zero.[/color]
      >
      > I am getting 0, both for "0.5" and 0.5 (the number). I'm using Opera 7,
      > what browser are you using?[/color]

      Oh, I see that now.
      [color=blue]
      > Hmm, let me check ... yes. Netscape 4:[/color]

      Yep. You are right! I often use NS4 to develope in. The most primitive
      possible case, so to speak. It also doesn't empty form fields on refresh,
      which can be a real time saver when hacking through forms.

      [color=blue]
      > parseInt(0.5)
      > gives NaN. The reason is that Netscape 4's toString turns 0.5 into the
      > string ".5", not "0.5" as the other browsers I checked.
      >[color=green]
      > > Is there another way of doing this[/color]
      >
      > Don't use numbers as arguments to parseInt, it's meant for strings.
      >
      > What are you trying to do?[/color]

      Convert fractional degrees to Degrees Minutes Seconds (like latitude).
      [color=blue]
      > If you want to turn a number into an integer, use Math.round, Math.floor
      > or Math.ceil.[/color]

      I need -0.5 to be 0. I may be relying too heavily on implied data type
      conversions

      Here's what I cobbled up:

      (To get minutes, you take the fractional part of degrees and multiply by 60.
      Same with seconds. I tried to keep this from line wrapping!)

      // parseInt but returns 0 from fractions instead of NaN
      function parseMyInt(n){ if((n > -1) && (n < 1)){return 0;}
      return parseInt(n);
      }

      // add leading 0 if needed
      function formatIt(n){ n=Math.abs(n);
      if(n <10){return n + '0'}
      return n;
      }

      // decimal degrees to D M S
      function
      imalToDMS(decim al){
      var D=parseMyInt(de cimal);

      var Mfraction=(deci mal - D) * 60;
      var M=parseMyInt(Mf raction);

      var Sfraction=Math. abs(Mfraction - M);

      M=formatIt(M);

      if((decimal < 0) && (D == 0)){D = '-0'} // fix for -0D

      var S=formatIt(Math .round(Sfractio n * 60));

      return{D:D,M:M, S:S};
      }

      Cheers,
      Jeff
      [color=blue]
      >
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.ht[/color]
      ml>[color=blue]
      > 'Faith without[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: parseInt .5

        "Jeff Thies" <nospam@nospam. net> writes:
        [color=blue][color=green]
        >> What are you trying to do?[/color]
        >
        > Convert fractional degrees to Degrees Minutes Seconds (like latitude).[/color]
        ....[color=blue]
        > I need -0.5 to be 0.[/color]

        function sign(n) {
        return n?n<0?-1:1:0;
        }
        function roundTowardsZer o(n) {
        return sign(n)*Math.fl oor(Math.abs(n) );
        }
        [color=blue]
        > I may be relying too heavily on implied data type conversions[/color]

        Probably :)
        [color=blue]
        > Here's what I cobbled up:
        >
        > (To get minutes, you take the fractional part of degrees and multiply by 60.
        > Same with seconds. I tried to keep this from line wrapping!)[/color]

        What is -10.5 degrees in DMS?
        -10 degrees and 30 minutes
        or
        -10 degrees and -30 minutes?
        You return it as
        {D:"-10",M:"30",S:"0 0"}

        I can do that too, but are you sure it is what you want? :)
        [color=blue]
        > // add leading 0 if needed
        > function formatIt(n){ n=Math.abs(n);[/color]

        Can it be negative? Should the sign be discarded if it is?


        Try this:
        ---
        function sign(n) {
        return n?n<0?-1:1:0;
        }
        function LZ(n) {
        return (n<10?"0":"")+n ;
        }

        function imalToDMS(decim al) {
        var ds = sign(decimal);
        var decimal = Math.abs(decima l);
        var D = Math.floor(deci mal);
        var Mfraction = (decimal - D) * 60;
        var M = Math.floor(Mfra ction);
        var Sfraction = (Mfraction - M) * 60;
        var S = Math.round(Sfra ction);
        return {D:(ds<0?"-":"")+D,M:LZ(M) ,S:LZ(S)};
        }
        ---

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Richard Cornford

          #5
          Re: parseInt .5

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:7k0rv6pp.f sf@hotpop.com.. .
          <snip>[color=blue][color=green]
          >>Convert fractional degrees to Degrees Minutes Seconds
          >>(like latitude).[/color]
          >...[color=green]
          >> I need -0.5 to be 0.[/color]
          >
          >function sign(n) {
          > return n?n<0?-1:1:0;
          >}
          >function roundTowardsZer o(n) {
          > return sign(n)*Math.fl oor(Math.abs(n) );
          >}[/color]
          <snip>

          If, and only if, the desired result can be expressed as a 32 bit signed
          integer (or maybe 31 bits for safety) it should also be possible to
          round a float towards zero by using an operator that would force the use
          of the internal ToInt32 function (such as any bitwise operator) in a way
          that doesn't otherwise change the number. Such as:-

          function roundTowardsZer o_Int32(n) {
          return (n|0);
          }

          Richard.


          Comment

          • Dr John Stockton

            #6
            Re: parseInt .5

            JRS: In article <SCZEb.6093$wL6 .327@newsread1. news.atl.earthl ink.net>,
            seen in news:comp.lang. javascript, Jeff Thies <nospam@nospam. net> posted
            at Sat, 20 Dec 2003 15:09:38 :-[color=blue]
            >
            >// add leading 0 if needed
            >function formatIt(n){ n=Math.abs(n);
            >if(n <10){return n + '0'}
            >return n;
            >}[/color]


            I think you need to spend considerably more time reading and testing,
            and less time writing.

            That function returns 50 for inputs of +5 & -5; and 00 for 0.

            function LZ(x) {return(x<0||x> 9?"":"0")+x} // for integers

            is reliable, and has the possible advantage of always returning a
            string.

            There is no leading zero function in the FAQ <FAQENTRY> bur perhaps
            there should be; one so often sees repetitive coding for it.

            <URL:http://www.merlyn.demo n.co.uk/js-maths.htm#LZ> refers.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...