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

S

Schklerg

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.
 
M

Michael Winter

Schklerg wrote on 25 Nov 2003:
I have this script performing a calculation on a page on my
site:
<script language="JavaScript">

You must specify the type attribute (type="text/javascript") in the
SCRIPT element. Type is sufficient and language is not needed.
function compute_weight(form)
{
var weight = form.wt.value;

Using the elements collection is more cross-browser compatible:

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

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?

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
 
D

DU

Michael said:
Schklerg wrote on 25 Nov 2003:

I have this script performing a calculation on a page on my
site:
<script language="JavaScript">


You must specify the type attribute (type="text/javascript") in the
SCRIPT element. Type is sufficient and language is not needed.

function compute_weight(form)
{
var weight = form.wt.value;


Using the elements collection is more cross-browser compatible:

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

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?


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;

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
 
P

PatD

I have this script performing a calculation on a page on my site: [cut]
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?

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 :)

Constructive comments welcome.


Yours
P
 
S

Schklerg

Thanks. That's just what I needed.

--
Jason
Remove nospam for email replies

DU said:
Michael said:
Schklerg wrote on 25 Nov 2003:

I have this script performing a calculation on a page on my
site:
<script language="JavaScript">


You must specify the type attribute (type="text/javascript") in the
SCRIPT element. Type is sufficient and language is not needed.

function compute_weight(form)
{
var weight = form.wt.value;


Using the elements collection is more cross-browser compatible:

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

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?


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;

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in Michael Winter <[email protected].
invalid> posted at Tue, 25 Nov 2003 13:47:14 :-
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;

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.
 
P

PatD

Albert Wagner said:
On Tue, 25 Nov 2003 15:54:55 +0100
I've personally always suffered from math phobia. But, somewhat OT, are
there good reasons for using only rational numbers in a given
calculation? I remember that the first versions of Smalltalk had only
rationals builtin.

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
 
M

Mike Scirocco

PatD said:
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

This works for me:

dropletcost = (dropletcost).toFixed(2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top