Link to home
Start Free TrialLog in
Avatar of Sathish David  Kumar N
Sathish David Kumar NFlag for India

asked on

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
Avatar of Ivo Stoykov
Ivo Stoykov
Flag of Bulgaria image

try this
HTH
Ivo Stoykov

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry mine code has error. Should be

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

Open in new window

why WScript.Echo???
alert will do just fine
@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>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is nicer to read:
String(val).indexOf('.')
than
(val + "").indexOf('.')
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
is typecasting is supported in javascript also????
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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");
 }
}
Yep :)
08 and 09 are invalid octal numbers
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.
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."));
but if i use " Math.round((parseInt( val,10))+"") != val" then for "1.0' it give it  1.0 is not a fraction
similarly 1.0 will give a fraction if we check for the index of '.'
will this not work
str= "1.0";
var myArray = str.match(/[0-9]*\.[1-9][0-9]*/g)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the point of
 if(Boolean(val.substring(val.indexOf(val)+1, val.length)))
???
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)))
to check for the value like "1."
Sorry, does not make any sense to me
Avatar of Sathish David  Kumar N

ASKER

any update !!
What do you mean.

Take one and try it, give points if happy.
I believe mine is complete http:a26200697
 
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 )
Thanks .....