This is a very simplified example of an Intranet application. But, is there an
easier (or more efficient) way of getting the decimal equivalent of a fraction?
The actual function gets the select values, this one is a simplified version
where its passed.
function checkIt(selVal) {
valueInDec1 = eval(selVal);
//do some calculations here with valueInDec1
}
<select onchange="check It(this.value)" >
<option value="0">0</option>
<option value="1/16">1/16</option>
<option value="1/8">1/8</option>
...............
<option value="7/8">7/8</option>
<option value="15/16">15/16</option>
</select>
I know that I could change it and have it look up the decimal equivalent for
the fraction, with an Object or an Array. I could also split the value on the /
and convert them to numbers, do the division, and get the decimal.
I discounted the Object/Array approach (and may go back to it, but doubt it).
The reason is that its not just 16ths. Some of the selects are 37ths, some are
7ths, one of them has 128ths in it. Simply put, I don't think that maintaining
10+ objects to contain the decimals is efficient (but open to that approach).
Also, its subject to get some added (different denominators), and some removed.
The split approach just doesn't seem to be as efficient though.
function checkIt(selVal) {
myNumber = selVal.split('/')
decimalForm = parseFloat(myNu mber[0])/parseFloat(myNu mber[1])
//do some calculations here with valueInDec1
}
When the form gets submitted, it has to be in fractional form. I have no
control over the way the selects get generated so I am kind of stuck trying to
do it client side.
Thoughts, suggestions, comments?
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
easier (or more efficient) way of getting the decimal equivalent of a fraction?
The actual function gets the select values, this one is a simplified version
where its passed.
function checkIt(selVal) {
valueInDec1 = eval(selVal);
//do some calculations here with valueInDec1
}
<select onchange="check It(this.value)" >
<option value="0">0</option>
<option value="1/16">1/16</option>
<option value="1/8">1/8</option>
...............
<option value="7/8">7/8</option>
<option value="15/16">15/16</option>
</select>
I know that I could change it and have it look up the decimal equivalent for
the fraction, with an Object or an Array. I could also split the value on the /
and convert them to numbers, do the division, and get the decimal.
I discounted the Object/Array approach (and may go back to it, but doubt it).
The reason is that its not just 16ths. Some of the selects are 37ths, some are
7ths, one of them has 128ths in it. Simply put, I don't think that maintaining
10+ objects to contain the decimals is efficient (but open to that approach).
Also, its subject to get some added (different denominators), and some removed.
The split approach just doesn't seem to be as efficient though.
function checkIt(selVal) {
myNumber = selVal.split('/')
decimalForm = parseFloat(myNu mber[0])/parseFloat(myNu mber[1])
//do some calculations here with valueInDec1
}
When the form gets submitted, it has to be in fractional form. I have no
control over the way the selects get generated so I am kind of stuck trying to
do it client side.
Thoughts, suggestions, comments?
--
Randy
All code posted is dependent upon the viewing browser
supporting the methods called, and Javascript being enabled.
Comment