> Programming Languages > Javascript
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Javascript



Javascript - "Formatting Number With Thousands Separator" in Programming Languages


Old 04-07-2004   #1
..ugl..
 
Default Formatting Number With Thousands Separator

Gday,


How would I format a number so that:

TheValue = 32500

Displays in the TextBox as: $32,500.00



Thanks a lot
-Douglas


 
Old 04-07-2004   #2
..n.. ....
 
Default Re: Formatting Number With Thousands Separator

Douglas wrote:

> Gday,
>
>
> How would I format a number so that:
>
> TheValue = 32500
>
> Displays in the TextBox as: $32,500.00
>


And if the person viewing the page uses . as a separator instead of ,
and uses , instead of . for the decimal?

But, for a hint, convert your number to a String, reverse the String,
then insert the separators, then reverse it back. And then read the FAQ
to find out how to add the 00 on the end (Its in there).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
 
Old 04-07-2004   #3
.. .... ..ockt..
 
Default Re: Formatting Number With Thousands Separator

JRS: In article <da-dnSh_pfanrendRVn-jg@comcast.com>, seen in
news:comp.lang.javascript, Randy Webb <hikksnotathome@aol.com> posted at
Wed, 7 Apr 2004 12:45:08 :
>Douglas wrote:


>> How would I format a number so that:
>>
>> TheValue = 32500
>>
>> Displays in the TextBox as: $32,500.00
>>

>
>And if the person viewing the page uses . as a separator instead of ,
>and uses , instead of . for the decimal?


Does any form of javascript recognise the possibility of a different
decimal separator or the possibility of a thousands separator (except by
added code, of course)?

If so, converting the number 12345.6 to string will allow then to be
determined.


>But, for a hint, convert your number to a String, reverse the String,
>then insert the separators, then reverse it back. And then read the FAQ
>to find out how to add the 00 on the end (Its in there).


There's no need to reverse the string.
<URL:http://www.merlyn.demon.co.uk/js-maths.htm#OutComma>

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<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.
 
Old 04-08-2004   #4
..s.. ..ichste.. ..els..
 
Default Re: Formatting Number With Thousands Separator

Dr John Stockton <spam@merlyn.demon.co.uk> writes:

> Does any form of javascript recognise the possibility of a different
> decimal separator or the possibility of a thousands separator (except by
> added code, of course)?


I can't say that there isn't some application of ECMAScript that doesn't
contain utility functions, but it's not part of ECMAScript, and it's not
in the usual clients.

> There's no need to reverse the string.
> <URL:http://www.merlyn.demon.co.uk/js-maths.htm#OutComma>


I notice the result -,123

If one only cares about recent versions of Javascript, regular
expressions with lokahead can be used to make it easier:
---
function tsep(n,swap) {
var ts=",", ds="."; // thousands and decimal separators
if (swap) { ts=","; ts="."; } // swap if requested

var ns = String(n),ps=ns,ss=""; // numString, prefixString, suffixString
var i = ns.indexOf(".");
if (i!=-1) { // if ".", then split:
ps = ns.substring(0,i);
ss = ds+ns.substring(i+1);
}
return ps.replace(/(\d)(?=(\d{3})+([.]|$))/g,"$1"+ts)+ss;
}
---

/L
--
L***e Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Old 04-08-2004   #5
..nn.. .. ..r..
 
Default Re: Formatting Number With Thousands Separator

In article <4074266f$0$27643$61ce578d@news.syd.swiftdsl.com.a u>,
Douglas <post.to.the.group@so.everyone.can.learn.com.au> wrote:

> Gday,
>
>
> How would I format a number so that:
>
> TheValue = 32500
>
> Displays in the TextBox as: $32,500.00
>
>
>
> Thanks a lot
> -Douglas
>
>

I took the challenge and wrote the following. If you find an error, let
me know.

<script type="text/javascript">

/// This script will format positive money values. P*** it a number
with or without decimal digits. It will be formatted with the currency,
thousands, and decimal symbols p***ed to it.
/// P***ED PARAMETERS
/// theNumber - the number to be formatted
/// theCurrency - the currency symbol
/// theThousands - the thousands separator
/// theDecimal - the decimal separator

function isThousands(position) {
if (Math.floor(position/3)*3==position) return true;
return false;
};

function formatMoney (theNumber,theCurrency,theThousands,theDecimal) {
var theDecimalDigits =
Math.round((theNumber*100)-(Math.floor(theNumber)*100));
theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
theNumber = ""+Math.floor(theNumber);
var theOutput = theCurrency;
for (x=0; x<theNumber.length; x++) {
theOutput += theNumber.substring(x,x+1);
if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1
!=0)) {
theOutput += theThousands;
};
};
theOutput += theDecimal + theDecimalDigits;
return theOutput;
};


alert(formatMoney(12345.67,"$",",","."))
alert(formatMoney(123456,"£",".",","))

</script>

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0