473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">
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.val ue = int_zero( weight * 2 )+(pgs/ppi)+0.5;
form.totht.valu e = int_zero( hite )+ 0.5;
form.spine.valu e = int_zero( pgs)/ (ppi);
form.hite1.valu e = int_zero(hite);
form.hite2.valu e = 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.011945392491 468. 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 4333
Schklerg wrote on 25 Nov 2003:
I have this script performing a calculation on a page on my
site:
<script language="JavaS cript">
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.011945392491 468. 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.011945392491 468 -> 13011.945392491 468
number = Math.round( number ); // 13011.945392491 468 -> 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
M.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #2
DU
Michael Winter wrote:
Schklerg wrote on 25 Nov 2003:

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

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.011945392491 468. 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.011945392491 468 -> 13011.945392491 468
number = Math.round( number ); // 13011.945392491 468 -> 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:

"toPrecisio n

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**********@i ntegratedbook.c om> 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.011945392491 468. 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.011945392491 468 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

Jul 20 '05 #4
On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.l u> 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*******@hotW IPETHISmail.com > wrote in message
news:bp******** **@news.eusc.in ter.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="JavaS cript">

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.011945392491 468. 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.011945392491 468 -> 13011.945392491 468
number = Math.round( number ); // 13011.945392491 468 -> 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:

"toPrecisio n

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.******@bluey onder.co.uk.
invalid> posted at Tue, 25 Nov 2003 13:47:14 :-

You could do this to round to 3dp:

number *= 1000; // 13.011945392491 468 -> 13011.945392491 468
number = Math.round( number ); // 13011.945392491 468 -> 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.demo n.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7
Albert Wagner <al******@tcac. net> wrote in message news:<200311251 00619.505ef6e1. al******@tcac.n et>...
On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.l u> 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.1428571428571 4285 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:<200311251 00619.505ef6e1. al******@tcac.n et>...

On Tue, 25 Nov 2003 15:54:55 +0100
PatD <do*****@pt.l u> 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.142857142857 14285 might talk to you, it doesn't to me.
"1 / 7" however is clearer... YMMV :)
Yours
P


This works for me:

dropletcost = (dropletcost).t oFixed(2);

Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12620
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from sysusers where name = 'public'; =========== -8.88178419
3
6354
by: shank | last post by:
I have the following equation. <% varWT = Round(CInt((rsFreePack.Fields.Item("Weight").Value)) + CInt(Session("w"))) %> Assume.... Round(CInt((rsFreePack.Fields.Item("Weight").Value)) = .12 CInt(Session("w")) = 30 How can I get it to always ROUND up to 31 ...?
10
12962
by: tmeister | last post by:
I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select round(123.465,2) returns
17
5665
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false
4
3451
by: Chris Davoli | last post by:
The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal = 526.5 CType(Math.Round(ldecWater, 0), String) -- Chris Davoli
7
4477
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545 -".number_format(20.545, 2, ',', '.')."<br>"; echo "number_format 20.555 -".number_format(20.555, 2, ',', '.')."<br>"; PHP Version 4.3.0 / FreeBSD
3
1837
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was expecting 12.2
4
10903
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
6
1259
by: Anthony | last post by:
Isn't this a mistake??? 3499.3494390340002 3499.3499999999999 3499.4000000000001 My Python 2.5.1 spat that out..
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.