Has anyone found a reliable way to force JS to round to a specific number of
places? Every time I try I get different results. For example, I'd need to
round 3.4589 to 2 places. What is the most reliable way to do it?
Thanks
-S 36 5912
Phat G5 (G3) said the following on 4/4/2006 6:35 PM: Has anyone found a reliable way to force JS to round to a specific number of places?
Yes.
Every time I try I get different results.
Then you aren't doing it right.
For example, I'd need to round 3.4589 to 2 places.
Hmmm. I seem to recall something in the group FAQ about rounding to 2
places.
<URL: http://jibbering.com/faq/#FAQ4_6 >
What is the most reliable way to do it?
Most reliable? Post in Usenet and ask how to do it to have someone point
you to the FAQ of the group you post it to.
--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
JRS: In article <C0************ *******@noone.c om>, dated Tue, 4 Apr
2006 15:35:52 remote, seen in news:comp.lang. javascript, Phat G5 (G3)
<no****@noone.c om> posted : Has anyone found a reliable way to force JS to round to a specific number of places? Every time I try I get different results. For example, I'd need to round 3.4589 to 2 places. What is the most reliable way to do it?
By reading the newsgroup FAQ before posting, and finding, IIRC, section
4.6 therein.
Your "most reliable" is a pointless term; either a method is reliable
(perhaps within stated limits) or it is wrong.
Remember the "Banker's Rounding" question, and the limitations on which
exact values a Number can take.
Of course, if your 3.4589 is a String, a different approach should be
considered.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
"Phat G5 (G3)" <no****@noone.c om> wrote in message
news:C05842D8.3 3E67%no****@noo ne.com... Has anyone found a reliable way to force JS to round to a specific number
of places? Every time I try I get different results. For example, I'd need to round 3.4589 to 2 places. What is the most reliable way to do it?
Some folks find it satisfactory to:
multiply the number by 100, then round it , then divide that by 100.
Some folks rather hassle a person over a well-meant question -
or a well-meant answer - like this.
( here it comes, I'm sure)
Hal Rosser wrote: "Phat G5 (G3)" [...] wrote [...] Has anyone found a reliable way to force JS to round to a specific number of places? Every time I try I get different results. For example, I'd need to round 3.4589 to 2 places. What is the most reliable way to do it?
Some folks find it satisfactory to: multiply the number by 100, then round it , then divide that by 100. Some folks rather hassle a person over a well-meant question - or a well-meant answer - like this. ( here it comes, I'm sure)
Your well-meant answer is simply bad advice. The FAQ (which is an acronym
for Frequently Asked Questions -- remember?) tells why, and understanding
how numeric values are stored in ECMAScript implementations , which we
discussed at great length and in great detail not too long ago, also does.
I got the impression that this newsgroup is dedicated to giving the best
advice possible, so do not be surprised if you get bashed when you post
(such) clueless nonsense.
PointedEars
"Hal Rosser" <hm******@bells outh.net> wrote in message
news:_2******** ****@bignews8.b ellsouth.net... "Phat G5 (G3)" <no****@noone.c om> wrote in message news:C05842D8.3 3E67%no****@noo ne.com... Has anyone found a reliable way to force JS to round to a specific
number of places? Every time I try I get different results. For example, I'd need
to round 3.4589 to 2 places. What is the most reliable way to do it?
Some folks find it satisfactory to: multiply the number by 100, then round it , then divide that by 100. Some folks rather hassle a person over a well-meant question - or a well-meant answer - like this. ( here it comes, I'm sure)
I'm not sure - but it looks like someone is saying its incorrect to round to
2 decimals with code
like this;
var num = 3.4589; // ***( number to be rounded to 2 decimals)
num = num * 100; //*** (Multiply the number by 100) num is now 345.89
num = Math.round(num) ; //*** (Then round it) num is now 346
num = num/100; // *** (then divide that number by 100) num is now 3.46
-- but I have seen the exact code in text books used in many schools.
Why is my (well-meaning) answer wrong ?
or should OP jump through 20 hoops before getting a straight answer from a
couple of mean-spirited ego hounds who think this group is the ultimate
source for javascript.
**never-mind**
---OP --- do what they say
Hal Rosser said on 06/04/2006 12:09 PM AEST: "Hal Rosser" <hm******@bells outh.net> wrote in message news:_2******** ****@bignews8.b ellsouth.net...
[...]Some folks find it satisfactory to: multiply the number by 100, then round it , then divide that by 100. Some folks rather hassle a person over a well-meant question - or a well-meant answer - like this. ( here it comes, I'm sure)
The FAQ provides a solution and explanation of why other methods are
faulty, hence other posters referenced the FAQ rather than attempt to
parrot or paraphrase it.
I'm not sure - but it looks like someone is saying its incorrect to round to 2 decimals with code like this;
Yes, they are.
var num = 3.4589; // ***( number to be rounded to 2 decimals) num = num * 100; //*** (Multiply the number by 100) num is now 345.89 num = Math.round(num) ; //*** (Then round it) num is now 346 num = num/100; // *** (then divide that number by 100) num is now 3.46 -- but I have seen the exact code in text books used in many schools.
The fact that something is written in a book does not make it good
advice. Some would say that any advice found in some books is almost
certainly wrong. ;-)
Why is my (well-meaning) answer wrong ?
The reason why it is wrong is explained in the FAQ - your proposed
solution does not always give the right answer. The short answer is
because JavaScript numbers can't represent all decimal values exactly,
depending on them to do so will fail some of the time.
e.g.
12.024999999999 999 -> 12.02 as expected
12.024999999999 9999 -> 12.03 ?
or should OP jump through 20 hoops before getting a straight answer from a couple of mean-spirited ego hounds who think this group is the ultimate source for javascript. **never-mind** ---OP --- do what they say
Read FAQ 4.6, it is rather concise but if studied it will all become
apparent. Also read 4.7, which helps to explain why the *100/100
method doesn't work consistently.
<URL:http://www.jibbering.c om/FAQ/#FAQ4_6>
If you have any specific questions, ask. Do not mind that some
responses are curt or abrupt - such is life.
Search the archives for questions on rounding - you may be surprised by
totally unrelated gems you discover. :-)
Or use:
<URL:http://www.merlyn.demo n.co.uk/js-round.htm>
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
// Roundoff routine for 2 decimal places
// used someplaces.
function round(x) {
return Math.round(x*10 0)/100;
}
Been using it for years in my raceway fill calculator at http://www.electrician2.com/
And never had a complaint.
<If you have any specific questions, ask. Do not mind that some
responses are curt or abrupt - such is life.
Search the archives for questions on rounding - you may be surprised by
totally unrelated gems you discover. :-) >
You are so anal your head is coming out of your ass. Just thought I
would let you know before you are seen in public. el*********@ele ctrician.com said on 06/04/2006 2:09 PM AEST: <If you have any specific questions, ask. Do not mind that some responses are curt or abrupt - such is life.
Search the archives for questions on rounding - you may be surprised by
totally unrelated gems you discover. :-) >
You are so anal your head is coming out of your ass. Just thought I would let you know before you are seen in public.
You're a very funny fellow. :-p
Funniest thing is not only don't you understand why you are wrong, but
you refuse to learn. You've posted incorrect responses before and had
the errors pointed out to you, yet you persist in offering bad advice.
Troll-on.
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matias Silva |
last post by:
Can anybody tell me why I am getting rounding errors
using the ROUND function.
3.7125 rounds to 3.70 when I use the following:
TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2)))
The correct value should be 3.71
I could round to the 3rd decimal place ROUND(X,3) and
that would round it correctly to 3.71 but that would
mean I would have to change the ROUND function in another
|
by: Penguin |
last post by:
At some long ago time Steve Jorgensen answered thus:
Subject: Re: How can I round a time?
Newsgroups: comp.databases.ms-access
Date: 1998/12/11
Access represents a date internally as a double and will convert
between
date/time and double automatically. The double value Access (or VB)
creates is based on 1 day = 1.0 and the fractional part represents a
|
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
|
by: Ronald W. Roberts |
last post by:
I'm having a problem understanding the Round function. Below are quotes
from two
books on VB.NET. The first book shows examples with one argument and
how it
rounds. The second book something different.
Programming Microsoft Windows with Microsoft Visual Basic.NET
"The Round method with a single argument return the whole number nearest
to the argument. If the argument to Round is midway between two whole
numbers,
|
by: Fuzzydave |
last post by:
I have been using a round command in a few places to round
a value to zero decimal places using the following format,
round('+value+', 0)
but this consistantly returns the rounded result of the value
to one decimal place with a zero
EG:
| |
by: David Coleman |
last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
|
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
|
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
|
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 !!!!
|
by: josh logan |
last post by:
Hello,
I need a round function that _always_ rounds to the higher integer if
the argument is equidistant between two integers. In Python 3.0, this
is not the advertised behavior of the built-in function round() as
seen below:
0
2
2
|
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...
| |
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |