Re: Round off



JRS: In article <2xkZf.319$Mf.218@xxxxxxxxxxxxxxxxxxxxxx>, dated Thu, 6
Apr 2006 22:59:22 remote, seen in news:comp.lang.javascript, Hal Rosser
<hmrosser@xxxxxxxxxxxxx> posted :

"Dr John Stockton" <jrs@xxxxxxxxxxxxxxxxxx> wrote in message
news:CSrcoUMz3YNEFwiH@xxxxxxxxxxxxxxxxxxxxx
JRS: In article <_2_Yf.82$Mf.27@xxxxxxxxxxxxxxxxxxxxxx>, dated Wed, 5
Perhaps you could ask your mathematics teachers about the conceptual
difference between rounding to a multiple of 0.01 giving a Number, and
rounding to two decimal places which necessarily gives a String.

******
The OP asked about rounding a number to 2 decimals - not about converting it
to a string.
***

"Two decimal places" implies a string. Number 3.5 is a multiple of
0.01, but it has only one decimal place. String "3.50" represents a
multiple of 0.01, and has two decimal places. They have the same
numerical value.


===========

In my system, somewhat to my surprise, Math.pow(10, N) is faster than
Number("1e"+N), and is sufficiently accurate[*] ; unless I hear that the
contrary is generally true, I'll change my pages.

[*] for (j=0; j<40; j++) document.writeln(j, " ", +("1E"+j), " ",
t=Math.pow(10, j), " ", t%1, "<br>")
My results for 23 & 29 need some more thought.

FAQ 4.6 can be correspondingly changed.

I'm sure that code is very clever and correct.
It is, however just so mush gibberish to someone who's trying to learn
something.

Don't quote what you're not commenting on. It confuses and annoys your
readers.

The FAQ author evidently did not feel that the comment presented in and
for StrU and its friends in js-round.htm needed to be repeated in the
FAQ, given that he was providing a link to js-round.htm. There, StrU
currently starts
function StrU(X, M, N) { // X>=0.0 ; gives M digits point N digits

Remember that the FAQ is posted twice a week, and that some users will
be on dial-up or wireless links. That is why (IMHO) the FAQ rightly
gives brief answers with links to further information.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
.



Relevant Pages

  • Re: Round off
    ... rounding to two decimal places which necessarily gives a String. ... The OP asked about rounding a number to 2 decimals - not about converting it ... FAQ 4.6 can be correspondingly changed. ...
    (comp.lang.javascript)
  • RE: prePEP: Decimal data type
    ... > the rounding procedure to be used in computations involving Euros ... implementing a fixed number of decimal digits "after the decimal point" ... To convert from the euro to the national currency, ... currency should then be rounded to two decimals. ...
    (comp.lang.python)
  • Re: Round off
    ... would likely come away from FAQ 4.2 with a sense of confusion. ... explanations on the art of rounding a number like 3.4589 to 2 decimals. ... Look at the 3rd example in the "General Rounding" section of that link: ... Both sites claim to be more than they are, they are misleading their audience if they propose a flawed method of rounding and do not point out the errors. ...
    (comp.lang.javascript)
  • Re: Decimal
    ... It is even calculating the decimals ... You can see that by clicking on a cell you entered data in and looking at the formula bar... ... You can truncate off any decimal value as if they were never typed in by the user in the first place, or you can round off the typed in value, but then there are two ways to do rounding in a macro, so you have to make sure you specify the right one. ...
    (microsoft.public.excel.misc)
  • Re: Decimall Float Question
    ... I would think that rounding is almost always done on the client to display numbers to the user. ... The posted code is unnecessarily concise and difficult to understand - senior posters here have stated that 'with' should not be used for just that reason, yet it is included in code posted on the FAQ. ... overly precise calculations would be pointless. ... I realise your advice is well intentioned, but it's not really suitable to recommend porting some unknown component of a Java/C/C++ library to JavaScript to answer a simple question. ...
    (comp.lang.javascript)