HighDots Forums  

FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2007-12-15)

Javascript JavaScript language (comp.lang.javascript)


Discuss FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2007-12-15) in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FAQ server
 
Posts: n/a

Default FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2007-12-15) - 12-14-2007 , 06:00 PM






-----------------------------------------------------------------------
FAQ Topic - How do I convert a Number into a String with
exactly 2 decimal places?
-----------------------------------------------------------------------

When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?

Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly. See section 4.7 for Rounding issues.

N = Math.round(N*100)/100 only converts N to a Number of value
close to a multiple of 0.01; but document.write(N) does not give
trailing zeroes.

ECMAScript Ed. 3.0 (JScript 5.5 [but buggy] and JavaScript 1.5)
introduced N.toFixed, the main problem with this is the bugs in
JScripts implementation.

Most implementations fail with certain numbers, for example 0.07.
The following works successfully for M>0, N>0:

function Stretch(Q, L, c) { var S = Q
if (c.length>0) while (S.length<L) { S = c+S }
return S
}
function StrU(X, M, N) { // X>=0.0
var T, S=new String(Math.round(X*Number("1e"+N)))
if (S.search && S.search(/\D/)!=-1) { return ''+X }
with (new String(Stretch(S, M+N, '0')))
return substring(0, T=(length-N)) + '.' + substring(T)
}
function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }
function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }
Number.prototype.toFixed= function(n){ return StrS(this,1,n)};

http://www.merlyn.demon.co.uk/js-round.htm

http://msdn.microsoft.com/library/de...34c0f6d6f0.asp


--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.


Reply With Quote
  #2  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2007-12-15) - 12-15-2007 , 11:40 AM






In comp.lang.javascript message <47631908$0$90270$14726298 (AT) news (DOT) sunsite.
dk>, Sat, 15 Dec 2007 00:00:01, FAQ server <javascript (AT) dotinternet (DOT) be>
posted:
Quote:
function Stretch(Q, L, c) { var S = Q
if (c.length>0) while (S.length<L) { S = c+S }
^^^^^^^^^^^^^^^ That seems unnecessary in this context. Indeed, the
loop could be incorporated in StrU.

Quote:
return S
}
function StrU(X, M, N) { // X>=0.0
var T, S=new String(Math.round(X*Number("1e"+N)))
^^^^^^^^^^^^^^ Math.pow is better
if (S.search && S.search(/\D/)!=-1) { return ''+X }
No need to .search; /\D/.test(St) is simpler
It would be better to left-pad that return value to width M+N+1

Quote:
with (new String(Stretch(S, M+N, '0')))
^^^^ ISTR an expressed dislike of using with.
^^^ ^^^^^^ there should be no need for a new string.

Quote:
return substring(0, T=(length-N)) + '.' + substring(T)
}

Quote:
Number.prototype.toFixed= function(n){ return StrS(this,1,n)};
Tatty spacing.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.


Reply With Quote
  #3  
Old   
Bart Van der Donck
 
Posts: n/a

Default Re: FAQ Topic - How do I convert a Number into a String with exactly2 decimal places? (2007-12-15) - 12-16-2007 , 04:21 AM



Dr J R Stockton wrote:

[...]

Quote:
Number.prototype.toFixed= function(n){ return StrS(this,1,n)};

Tatty spacing.
Maybe this is an occasion on how whitespace (and by extension) line-
end are handled in the daily FAQ-postings.

The nature of the application requires to start from
http://www.jibbering.com/faq/index.xml with all whitespaces and line-
ends as written in the source code.

This is not like browser displays where whitespace and EOL are treated
differently.

I had invented the following regular expressions in order to obtain a
(IMO) best possible Usenet-display:

"p" => "\n",
"/p" => "\n",
"em" => "_",
"/em" => "_",
"url" => "\n\n",
"/url" => "\n\n",
"ul" => "\n",
"/ul" => "\n",
"li" => "* ",
"/li" => "",
"moreinfo" => "\n\n",
"/moreinfo" => "\n\n",
"resource" => "\n\n",
"/resource" => "\n\n",
"icode" => "` ",
"/icode" => " `",
"code" => "\n\n",
"/code" => "\n\n",

Additional rules:
- If more than three successive end-of-lines characters occur, they
are replaced by two.
- Six trailing spaces are removed.
- End-of-lines at the beginning an end of every message are removed.

Sometimes this may lead to less-than-optimal results, but I'm afraid
there is no other possibility because the application needs some rules
anyway how it should display the input stream.

Quick link to the results:
http://groups.google.com/groups/sear...be&scor ing=d

For all clarity, this does not apply to the code line that you
mentioned as this is literally the same as the source.

--
Bart


Reply With Quote
  #4  
Old   
Bart Van der Donck
 
Posts: n/a

Default Re: FAQ Topic - How do I convert a Number into a String with exactly2 decimal places? (2007-12-15) - 12-16-2007 , 04:24 AM



Bart Van der Donck wrote:

Quote:
Additional rules:
- If more than three successive end-of-lines characters occur, they
are replaced by two.
- Six trailing spaces are removed.
I meant leading spaces.

--
Bart


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.