On 02/05/2005 19:12, SoniC SouL wrote:
thats great. i just dont want the crazy rouding to appear. what do you
suggest i do to show the result as currency ?
Perform the operations in pennies/cents/whatever so that you only use
integers. You can then use the function below to create a string
representing the value in pounds/dollars/whatever.
/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* t - Thousands separator (defaults to none).
* d - Decimal separator (defaults to '.').
*
* Outputs a number of the form cntnnntnnndnn
*
* For example, toCurrency(142635.7, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, t, d) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.round(n));
var j, i = '', f; c = c || ''; t = t || ''; d = d || '.';
while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = t + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + d + f;
}
Notice the rounding of the floating-point value in the example.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.