Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?

Reply
Thread Tools

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

 
 
FAQ server
Guest
Posts: n/a
 
      04-13-2007
-----------------------------------------------------------------------
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 ? '-' : ''; }
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.

 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      04-15-2007
In comp.lang.javascript message <46200b76$0$90270$.
dk>, Fri, 13 Apr 2007 23:00:01, FAQ server <>
posted:

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


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


but it is stale code.

> 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 ? '-' : ''; }
> function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }
> Number.prototype.toFixed= function(n){ return StrS(this,1,n)};


StrS has its uses, but so do StrT and StrW.

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


It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
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
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? FAQ server Javascript 0 08-14-2007 11:00 PM
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? FAQ server Javascript 0 06-13-2007 11:00 PM
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? FAQ server Javascript 28 02-23-2007 12:46 AM
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? FAQ server Javascript 52 02-07-2007 04:57 PM
FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? FAQ server Javascript 6 11-03-2006 11:14 PM



Advertisments