Solved

how do I check the sign of a java script variable ?

Posted on 2011-09-10
3
306 Views
Last Modified: 2012-05-12
how do I check the sign of a java script variable ?

Here is my input/output to illustrate the problem.

Example Input/output:
Input:  var temp="-2"
Output:  This is a negative number . absolute value is =2

Input:  var temp="2"
Output:  This is a positive number . absolute value is 2

Input:  var temp="0"
Output:  This is a zero . absolute value is 0

I'm worried as javascript variable are unsigned . How do I check for a negative number then like above ?
0
Comment
Question by:cofactor
  • 2
3 Comments
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36516238
check if it less than 0, then multiply by -1
then you will get absolute..
0
 
LVL 13

Accepted Solution

by:
ansudhindra earned 100 total points
ID: 36516239
or you can do it like
var t=parseInt("-7");
if(t<0) { t=t*-1; }

alert("absolute value is "+t);
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36516475
var temp="-2"
var tempIsNegative = !(temp == Math.abs(temp)); // true or false
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now