Thanks, but that doesn't round to the nearest 1/16. I need the function to round to the neartest 1/16th.
Main Topics
Browse All TopicsI need a cut and paste function to convert an existing value that is stored as a decimal to display as a fraction, rounding to the nearest 1/16. I.E., if I have a value of .062 or .0625 or something close, I want to display 1/16 to the user. The application this is for is written in ColdFusion with client side javascript. I would prefer the function to be in JavaScript, but if someone already has one written in ColdFusion, that's cool too. If anyone needs any more info, please let me know. Thank you.
Jason
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Try this ...
<script language="javascript">
var Num = 1, Den = 16;
var RoundTo = 0.0625;
var Number = 0.6;
function ReduceNumDen()
{
var i;
for (i = 2 ; i <= Den ; i++)
{
while (((Den % i) == 0) && ((Num % i) == 0))
{
Den /= i;
Num /= i;
}
}
}
function CalculateFractions(Number,
{
var Multiplier = 1;
if (Number < RoundTo)
{
alert("Error");
return;
}
while (Multiplier * RoundTo < Number)
{
Multiplier++;
}
if (Multiplier * RoundTo > Number)
{
if (Math.abs(Multiplier * RoundTo - Number) > Math.abs((Multiplier - 1) * RoundTo - Number))
{
Multiplier--;
}
}
Num *= Multiplier;
ReduceNumDen();
}
CalculateFractions(Number,
alert(Num + "/" + Den);
</script>
Business Accounts
Answer for Membership
by: rolftollerudPosted on 2003-08-05 at 13:18:14ID: 9083422
Found this at this location: http://www.csgnetwork.com/ decimal2fr actwecalc. html
ue = dec; // insert below ue = div; // insert below
ue; // numerator ue; // denominator al.value = dec; // value
Should at least help you on the way.
<SCRIPT language=JavaScript>
<!--
function decfrac()
{
var form = window.document.decform;
var dec = form.dec.value;
var decString = dec.toString();
var dslength = decString.length - 1;
var div = 1;
// Multiply by powers of 10 to remove the decimal
for (i=0; i<dslength; i++)
{
dec = dec * 10;
div = div * 10;
}
// Factor out the GCF of the two numbers
for (i=2; i <= dec; i++) {
while ((mod(dec,i) == 0) && (mod(div,i) == 0))
{
dec = dec/i;
div = div/i;
}
}
// Display the two numbers in the fraction form boxes and in the verification form.
form.numerator.value = dec;
form.denominator.value = div;
window.document.calc.t.val
window.document.calc.b.val
}
function mod(n, m)
{
while (n >= m)
{
n = n-m;
}
return(n);
/*Keeps subtracting the number you want to divide by until the remainder is less than the original divisor, and then returning the remainder.
*/
}
function fracdec()
{
var t = window.document.calc.t.val
var b = window.document.calc.b.val
var dec = t / b; // calculation
window.document.calc.decim
/*Verifies the upper calculator as it best can. On works on terminating fractions. It approximates the others.
*/
}
//-->
</SCRIPT>