470,614 Members | 1,396 Online

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

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.

--
Jason
Remove nospam for email replies
Jul 20 '05 #1
8 4175 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;

<snip>
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

--
Michael Winter
Jul 20 '05 #2
Michael Winter wrote:
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;

<snip>
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/...r.html#1201389

Also, you may want to look at toFixed
http://devedge.netscape.com/library/...r.html#1200964
DU

Jul 20 '05 #3
On Tue, 25 Nov 2003 08:25:54 -0500, "Schklerg"
<ja**********@integratedbook.com> wrote:
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 :)

Yours
P

Jul 20 '05 #4
On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.lu> wrote:
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.
--
Then there was the man who drowned crossing a stream with an average
depth of six inches.
-- W. I. E. Gates
Jul 20 '05 #5
Thanks. That's just what I needed.

--
Jason
Remove nospam for email replies

"DU" <dr*******@hotWIPETHISmail.com> wrote in message
news:bp**********@news.eusc.inter.net...
Michael Winter wrote:
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;

<snip>
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/...r.html#1201389
Also, you may want to look at toFixed
http://devedge.netscape.com/library/...r.html#1200964

DU

Jul 20 '05 #6
JRS: In article <Xn*******************************@193.38.113.46 >, seen
in news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.uk.
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.

--
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
Jul 20 '05 #7
Albert Wagner <al******@tcac.net> wrote in message news:<20031125100619.505ef6e1.al******@tcac.net>.. .
On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.lu> wrote:
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
Jul 20 '05 #8
PatD wrote:
Albert Wagner <al******@tcac.net> wrote in message news:<20031125100619.505ef6e1.al******@tcac.net>.. .

On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.lu> wrote:
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

This works for me:

dropletcost = (dropletcost).toFixed(2);

Jul 20 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion.