# Can I round or set the number of decimals or even make this a fraction?

Discussion in 'Javascript' started by Schklerg, Nov 25, 2003.

1. ### SchklergGuest

I have this script performing a calculation on a page on my site:
<script language="JavaScript">
function compute_weight(form)
{
var weight = form.wt.value;
var pgs = form.pgs.value;
var ppi = form.ppi.value;
var hite = form.ht.value;
if (weight > 0.0) {
form.totwid.value = int_zero( weight * 2 )+(pgs/ppi)+0.5;
form.totht.value = int_zero( hite )+ 0.5;
form.spine.value = int_zero( pgs)/ (ppi);
form.hite1.value = int_zero(hite);
form.hite2.value = int_zero(hite);
form.wid1.value = int_zero(weight);
form.wid2.value = int_zero(weight);
form.spinetype.value = int_zero( pgs)/ (ppi) -.125;
}
}
// Function to return 0 if result is <1
function int_zero(x)
{
if ( x < 1 )
return 0 ;
else
return parseInt( x ,10 );
}
</script>
At times, depending on the values entereed into my form, I can end up with a
number like 13.011945392491468. 15 decimal places is just too confusing.
Is there a way I can round this to 3? Or even better, is there away to make
this value appear as a fraction? If you need more code from the page, it's
at www.integratedbook.com/pbkcalc.html. Thanks for your help.

Schklerg, Nov 25, 2003

2. ### Michael WinterGuest

Schklerg wrote on 25 Nov 2003:
You must specify the type attribute (type="text/javascript") in the
SCRIPT element. Type is sufficient and language is not needed.
Using the elements collection is more cross-browser compatible:

var = weight = form.elements['wt'].value;

You could do this to round to 3dp:

number *= 1000; // 13.011945392491468 -> 13011.945392491468
number = Math.round( number ); // 13011.945392491468 -> 13012.0
number /= 1000; // 13012.0 -> 13.012

or

number = Math.round( number * 1000 ) / 1000;

Displaying a fraction is much more difficult. After looking at your
site, I don't think it would be particularly appropriate, either.

Mike

Michael Winter, Nov 25, 2003

3. ### DUGuest

or use toPrecision method which involves only 1 call, 1 line:

"toPrecision

Returns a string representing the Number object to the specified precision.
"
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1201389

Also, you may want to look at toFixed
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964

DU

DU, Nov 25, 2003
4. ### PatDGuest

Fractions? Well... depends on your numbers and the precision required.

Your 13.011945392491468 is "close enough" approximated by 7625 / 586.

Now, whether that's good enough for you,
or, maybe, the numbers are "too large" already?

Anyway, you may want to have a look at this:

function q(n)
{
var x = n;
var a = Math.floor(n);
var p0 = 1;
var q0 = 0;
var p1 = a;
var q1 = 1;
var p2 = p1;
var q2 = q1;

while(x - a && Math.abs(n - p2 / q2) > 1e-10)
{
x = 1 / (x - a);
a = Math.floor(x);
p2 = a * p1 + p0;
q2 = a * q1 + q0;
p0 = p1;
q0 = q1;
p1 = p2;
q1 = q2;
}
return p2 + " / " + q2;
}

Should return a fractional "string" of anything you might want
to throw at it

Yours
P

PatD, Nov 25, 2003
5. ### SchklergGuest

Thanks. That's just what I needed.

--
Jason
Remove nospam for email replies

Schklerg, Nov 25, 2003
6. ### Dr John StocktonGuest

JRS: In article <[email protected]>, seen
in Michael Winter <.
invalid> posted at Tue, 25 Nov 2003 13:47:14 :-
Those round to a multiple of 0.001, which is not exactly the same thing
as rounding to three decimal places.

The OP is clearly outputting the number for eyeball input, in other
words to a string. One-and-a-tenth, rounded to a multiple of 0.001, is a
number of value (close to) 1.1; but, as a string to three decimal
places, it must be "1.100".

FAQ, section 4.6,
and <URL:http://www.merlyn.demon.co.uk/js-round.htm>

Note : toFixed and toPrecision are not available in all Web browsers,
and toFixed (at least) has bugs.

Dr John Stockton, Nov 25, 2003
7. ### PatDGuest

Don't know for the "good" reasons, but, as an example,
0.14285714285714285 might talk to you, it doesn't to me.
"1 / 7" however is clearer... YMMV

Yours
P

PatD, Nov 26, 2003
8. ### Mike SciroccoGuest

This works for me:

dropletcost = (dropletcost).toFixed(2);

Mike Scirocco, Nov 30, 2003