• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

check whether the number having is fractional value or not in java script

check whether the number having is fractional value or not  in java script
0
Sathish David  Kumar N
Asked:
Sathish David  Kumar N
  • 10
  • 7
  • 5
  • +2
5 Solutions
 
Ivo StoykovCommented:
try this
HTH
Ivo Stoykov

var val = 74.5;
alert((val % 10 != 0 ? 'has ' : '')+'fraction') ;

Open in new window

0
 
Gurvinder Pal SinghCommented:
something like this
function check()
      {
            var val1 = "5.5";
            if ( Math.round(parseInt( val1)) != val1)
            {
                  alert("it is a fractional value");
            }
            else
            {
                  alert("It is not");
            }
      }
0
 
Ivo StoykovCommented:
sorry mine code has error. Should be

var val = 74;
WScript.Echo((val % 2 != 0 ? 'has' : 'no')+' fraction') ;

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Michel PlungjanIT ExpertCommented:
why WScript.Echo???
alert will do just fine
0
 
Michel PlungjanIT ExpertCommented:
@gurvinder

<script>
function check(val1) {
  if ( Math.round(parseInt( val1)) != val1) {
    alert("it is a fractional value");
  }
  else {
    alert("It is not");
  }
}
check("09"); // oops?
</script>
0
 
manjunathubCommented:
function check(val){
            if(! isNaN(val)){  //check the passed value is number or not
                    if( (val + "").indexOf('.') != -1)
                                     alert(val + " is  a fraction");
                  else
                                     alert(val + " is not a fraction");
         }
         else{
                  alert(val + " is not a number");
         }
}
0
 
Michel PlungjanIT ExpertCommented:
This is nicer to read:
String(val).indexOf('.')
than
(val + "").indexOf('.')
0
 
Gurvinder Pal SinghCommented:
but this will not tell me if this is a valid number at first place since 1.1.1.3 will also qualify as a fraction
0
 
manjunathubCommented:
is typecasting is supported in javascript also????
0
 
Michel PlungjanIT ExpertCommented:
Gurvinder: 1.1.1.3 is not a number

<script>
function isFraction(val){
  if(isNaN(val)) {
    alert(val + " is not a number");
    return false;  
  }
  return String(val).indexOf('.') != -1;
}

alert("1.2 is a fraction:"+isFraction(1.2));
alert("1.2.2.2 is a fraction:"+isFraction("1.2.2.2"));
alert("1222 is a fraction:"+isFraction("1222"));

</script>

Open in new window

0
 
Gurvinder Pal SinghCommented:
sorry, it has to be
function check(val1)
{
 alert(Math.round(parseInt( val1,10)));
 if ( Math.round(parseInt( val1, 10)) != val1) {
   alert("it is a fractional value");
 }
 else {
   alert("It is not");
 }
}
0
 
Michel PlungjanIT ExpertCommented:
Yep :)
08 and 09 are invalid octal numbers
0
 
manjunathubCommented:
Here 08 and 09 are considered a decimal number and v r checking 4r number is fraction or not, not 4r the octal or hexadecimal number.
0
 
Michel PlungjanIT ExpertCommented:
My comment was directed at Gurvinder, as a response to his response on my comment http:#a26198191
Your script would work since you a) do a test for isNaN and b) test the string for a "."
However your script also tells me 1. is a fraction..

alert("1. is a fraction:"+isFraction("1."));
0
 
manjunathubCommented:
but if i use " Math.round((parseInt( val,10))+"") != val" then for "1.0' it give it  1.0 is not a fraction
0
 
Gurvinder Pal SinghCommented:
similarly 1.0 will give a fraction if we check for the index of '.'
0
 
Gurvinder Pal SinghCommented:
will this not work
str= "1.0";
var myArray = str.match(/[0-9]*\.[1-9][0-9]*/g)
0
 
Michel PlungjanIT ExpertCommented:
Ok here



<script>
function isFraction(val){
  if(isNaN(val)) {
    alert(val + " is not a number");
    return false;  
  }
  return val-parseInt(val,10)>0;
}

document.write("<br>1.2 is a fraction:"+isFraction(1.2));
document.write("<br>1.2.2.2 is a fraction:"+isFraction("1.2.2.2"));
document.write("<br>1222 is a fraction:"+isFraction("1222"));
document.write("<br>1. is a fraction:"+isFraction("1."));
document.write("<br>1.0 is a fraction:"+isFraction("1.0"));
document.write("<br>1.02 is a fraction:"+isFraction("1.02"));

</script>

Open in new window

0
 
manjunathubCommented:
ok check this one
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


<script language="javascript">
function isFraction(val){
	
	val=val+"";
	if(! isNaN(val)){  //check the passed value is number or not
		if(Boolean(val.substring(val.indexOf(val)+1, val.length))){
			  if( (val + "").indexOf('.') != -1)
						 return val + " is  a fraction";
			else
						 return val + " is not a fraction";
		}
		else
			return val + " is not a fraction";
	   }
	   else{
			return val + " is not a number";
	   }
}
	
	
	
</script>
</head>
<body>
<script>
	document.write(isFraction("1.0") + "<br/>");
	document.write(isFraction("1.0.0")+ "<br/>");
	document.write(isFraction("1.")+ "<br/>");
	document.write(isFraction("1")+ "<br/>");
	document.write(isFraction("01")+ "<br/>");
	document.write(isFraction("0x24.5")+ "<br/>");
</script>
</body>
</html>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
What is the point of
 if(Boolean(val.substring(val.indexOf(val)+1, val.length)))
???
0
 
manjunathubCommented:
if condition should be if(Boolean(val.substring(val.indexOf(val)+1, val.length-1))) not
if(Boolean(val.substring(val.indexOf(val)+1, val.length)))
0
 
manjunathubCommented:
to check for the value like "1."
0
 
Michel PlungjanIT ExpertCommented:
Sorry, does not make any sense to me
0
 
Sathish David Kumar NAuthor Commented:
any update !!
0
 
Michel PlungjanIT ExpertCommented:
What do you mean.

Take one and try it, give points if happy.
I believe mine is complete http:a26200697
 
0
 
Sathish David Kumar NAuthor Commented:
sorry mplungjan i select ur code as multiple selection but its not apper here i dont know how to revert back the point ..
can u put request attention i will assign point to u !! ( ur code also helped me )
0
 
Sathish David Kumar NAuthor Commented:
Thanks .....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 7
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now