Click to See Complete Forum and Search --> : Easy Math Problem


sean_the_hacker
09-20-2003, 05:53 PM
I am a begginer of JavaScript and I was just writing a simple fraction to decimal conversion page; but I had a few problems.
When I added two of the variables together it does this

Say I had the fraction 1 1/2, and I tried to convert it, this is what happens:

it comes up with 10.5 as the answer, because it adds the variables, instead of adding the values of the variables.


Any sugggestions?

S1L3NC3
09-20-2003, 05:59 PM
If im not mistaken, i think you need to use something along the lines of the val function.

like val(variable1) + val(variable2)

Just a thought, dont know if that will help or not.

Charles
09-20-2003, 06:11 PM
Originally posted by sean_the_hacker
Any sugggestions? <script type="text/javascript">
<!--
function parseFraction (s) {
s.match(/([+\-])?(\d*)\s*((\d+)\/(\d+))?/)
return RegExp.$1 == '-' ? 0 - Number(Number (RegExp.$2) + Number (RegExp.$4) / Number (RegExp.$5)) : Number(Number (RegExp.$2) + Number (RegExp.$4) / Number (RegExp.$5))
}

alert (parseFraction('-12 1/2'))
// -->
</script>