By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

parseInt .5

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Jeff Thies" <no****@nospam.net> writes:
I've noticed that doing:

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

parsInt(x)

yields NaN.

I was expecting zero.
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.
Is there another way of doing this


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
> > I've noticed that doing:

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

parsInt(x)

yields NaN.

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


Oh, I see that now.
Hmm, let me check ... yes. Netscape 4:
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.

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.
Is there another way of doing this
Don't use numbers as arguments to parseInt, it's meant for strings.

What are you trying to do?


Convert fractional degrees to Degrees Minutes Seconds (like latitude).
If you want to turn a number into an integer, use Math.round, Math.floor
or Math.ceil.
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(decimal){
var D=parseMyInt(decimal);

var Mfraction=(decimal - D) * 60;
var M=parseMyInt(Mfraction);

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

M=formatIt(M);

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

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

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

Cheers,
Jeff

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.ht ml> 'Faith without

Jul 20 '05 #3

P: n/a
"Jeff Thies" <no****@nospam.net> writes:
What are you trying to do?
Convert fractional degrees to Degrees Minutes Seconds (like latitude).

.... I need -0.5 to be 0.
function sign(n) {
return n?n<0?-1:1:0;
}
function roundTowardsZero(n) {
return sign(n)*Math.floor(Math.abs(n));
}
I may be relying too heavily on implied data type conversions
Probably :)
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!)
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:"00"}

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


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(decimal) {
var ds = sign(decimal);
var decimal = Math.abs(decimal);
var D = Math.floor(decimal);
var Mfraction = (decimal - D) * 60;
var M = Math.floor(Mfraction);
var Sfraction = (Mfraction - M) * 60;
var S = Math.round(Sfraction);
return {D:(ds<0?"-":"")+D,M:LZ(M),S:LZ(S)};
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:7k**********@hotpop.com...
<snip>
Convert fractional degrees to Degrees Minutes Seconds
(like latitude).

...
I need -0.5 to be 0.


function sign(n) {
return n?n<0?-1:1:0;
}
function roundTowardsZero(n) {
return sign(n)*Math.floor(Math.abs(n));
}

<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 roundTowardsZero_Int32(n) {
return (n|0);
}

Richard.
Jul 20 '05 #5

P: n/a
JRS: In article <SC****************@newsread1.news.atl.earthlink.n et>,
seen in news:comp.lang.javascript, Jeff Thies <no****@nospam.net> posted
at Sat, 20 Dec 2003 15:09:38 :-

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

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.demon.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.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.