Objectmix
Tags Register Mark Forums Read

FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12) : Javascript

This is a discussion on FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12) within the Javascript forums in Programming Languages category; ----------------------------------------------------------------------- FAQ Topic - How do I format a Number as 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 the section on "simple decimal arithmetic" 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 ` Number.prototype.toFixed `, ...


Object Mix > Programming Languages > Javascript > FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12)

Reply

 

LinkBack Thread Tools
  #1  
Old 11-11-2008, 07:00 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12)

-----------------------------------------------------------------------
FAQ Topic - How do I format a Number as 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 the section on "simple decimal arithmetic"
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 ` Number.prototype.toFixed `, the main problem
with this is the bugs in JScript's 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 = String(Math.round(X * Number("1e" + N)));
if (S.search && S.search(/\D/) != -1) {
return '' + X;
}
with (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://msdn2.microsoft.com/en-us/library/sstyff0z.aspx


--
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 11-11-2008, 08:39 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: FAQ Topic - How do I format a Number as a String with exactly 2decimal places? (2008-11-12)

What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43

Reply With Quote
  #3  
Old 11-11-2008, 08:40 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: FAQ Topic - How do I format a Number as a String with exactly 2decimal places? (2008-11-12)

What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43

Reply With Quote
  #4  
Old 04-20-2009, 04:26 AM
Junior Member
 
Join Date: Apr 2009
Posts: 1
kinogam is on a distinguished road
Cool Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (

var numstr = "3.1415926";

alert(numstr.replace(/(^\d+(?:\.\d{2})?)\d*/,"$1"));


//how about this?
Reply With Quote
Reply

Thread Tools



All times are GMT -5. The time now is 01:54 PM.