Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can I round or set the number of decimals or even make this a fraction?

Reply
Thread Tools

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

 
 
Schklerg
Guest
Posts: n/a
 
      11-25-2003
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


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      11-25-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d (remove ".invalid" to reply)
 
Reply With Quote
 
 
 
 
DU
Guest
Posts: n/a
 
      11-25-2003
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

 
Reply With Quote
 
PatD
Guest
Posts: n/a
 
      11-25-2003
On Tue, 25 Nov 2003 08:25:54 -0500, "Schklerg"
<(E-Mail Removed)> 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

Constructive comments welcome.


Yours
P

 
Reply With Quote
 
Schklerg
Guest
Posts: n/a
 
      11-25-2003
Thanks. That's just what I needed.

--
Jason
Remove nospam for email replies

"DU" <(E-Mail Removed)> wrote in message
news:bpvn31$alo$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-25-2003
JRS: In article <Xns943E8C403462EMWinterBlueyonder@193.38.113.46 >, seen
in news:comp.lang.javascript, Michael Winter <(E-Mail Removed).
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
PatD
Guest
Posts: n/a
 
      11-26-2003
Albert Wagner <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
> On Tue, 25 Nov 2003 15:54:55 +0100
> PatD <(E-Mail Removed)> 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
 
Reply With Quote
 
Mike Scirocco
Guest
Posts: n/a
 
      11-30-2003
PatD wrote:

>Albert Wagner <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>.. .
>
>
>>On Tue, 25 Nov 2003 15:54:55 +0100
>>PatD <(E-Mail Removed)> 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);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
So, round and round... 70-682... MSLearning, please read! DaGenester MCITP 2 05-20-2010 04:36 PM
VS2005 - Going round and round in circles again :) postings@alexshirley.com ASP .Net 0 06-22-2006 11:26 AM
Float.round - should it be round-to-even OliverMarchand Ruby 2 04-12-2006 12:06 PM
Round Decimals Jerry Java 9 08-12-2005 02:47 PM
Computer Goes Round and Round Checking Alan Computer Support 6 10-08-2004 08:24 AM



Advertisments