Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Abbreviate Currency using Javascript

0 views
Skip to first unread message

Sunny

unread,
Sep 26, 2008, 1:50:03 PM9/26/08
to
Hi,

Is there a way in Javascript to abbreviate currency.
Like if it is $1000 then it convert it into 1K.

Thomas 'PointedEars' Lahn

unread,
Sep 26, 2008, 3:37:03 PM9/26/08
to
Sunny wrote:
> Is there a way in Javascript to abbreviate currency.

Yes.

> Like if it is $1000 then it convert it into 1K.

Currency is but a number and a unit. What have you tried?


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

Sunny

unread,
Sep 26, 2008, 4:29:12 PM9/26/08
to
On Sep 26, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> -- Richard Cornford, cljs, <f806at$ail$1$8300d...@news.demon.co.uk>

Well, I dont know, from where to start?
I cant think of anything.
Can you provide me a starting point.

Stevo

unread,
Sep 26, 2008, 4:38:54 PM9/26/08
to

Sure, it's easy.

function convertCurrency(a)
{
if(a=="$1000")
{
a="1K";
}
return a;
}

window.alert(convertCurrency("$1000")); //alert 1K

Conrad Lender

unread,
Sep 26, 2008, 5:02:20 PM9/26/08
to
On 2008-09-26 22:38, Stevo wrote:
>> Is there a way in Javascript to abbreviate currency.
>> Like if it is $1000 then it convert it into 1K.
>
> Sure, it's easy.
>
> function convertCurrency(a)
> {
> if(a=="$1000")
> {
> a="1K";
> }
> return a;
> }

That would fail if the amount was "$2000".
May I suggest an improved version?

function convertCurrency (a) {
if (a == "$1000") {
return "1K";
} else if (a == "$2000") {
return "2K";
}
return a;
}


- Conrad

Thomas 'PointedEars' Lahn

unread,
Sep 26, 2008, 5:10:00 PM9/26/08
to
Sunny wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Sunny wrote:
>>> Is there a way in Javascript to abbreviate currency.
>> Yes.
>>
>>> Like if it is $1000 then it convert it into 1K.
>> Currency is but a number and a unit. What have you tried?
>>
>> [snipped quoted signature]

>
> Well, I dont know, from where to start?
> I cant think of anything.
> Can you provide me a starting point.

<http://jibbering.com/faq/>
<http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/replace>


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

Conrad Lender

unread,
Sep 26, 2008, 5:10:59 PM9/26/08
to
On 2008-09-26 22:29, Sunny wrote:
> On Sep 26, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>>> Is there a way in Javascript to abbreviate currency.
>> Yes.
>>> Like if it is $1000 then it convert it into 1K.
>> Currency is but a number and a unit. What have you tried?

> Well, I dont know, from where to start?


> I cant think of anything.
> Can you provide me a starting point.

You usually start by defining what you're going for. Take some example
input and output values, then think about how you could generalize what
you've been doing by hand into an algorithm. For example:

amount format
------------------------
0 ?
0.4 ?
-4 ?
999 ?
1000 ?
1001 ?
100000000000000 ?

You weren't giving enough information for us to be able to help you.
Many, many people have implemented pretty-print functions like the one
you're probably looking for (I know I have). The question is, what
exactly are you looking for, and have you tried anything to solve your
problem?


- Conrad

Dr J R Stockton

unread,
Sep 27, 2008, 11:01:02 AM9/27/08
to
On Sep 26, 6:50 pm, Sunny <sunnyluth...@gmail.com> wrote:

> Is there a way in Javascript to abbreviate currency.
> Like if it is $1000 then it convert it into 1K.

To answer your question : Yes, there is; in fact, there are many.

A single example is inadequate to discriminate between possible
requirements.

One can only have $1000 in JavaScript if it is a String. In any
normal situation, one should start with the calculated Number.

But consider

S = "$1000000000"
S = S.replace(/(0+)$/, function(a, s) { var L = s.length
return ['','0','00'][L%3] + ['','K','M','G','T','P','E'][(L/3)|0]})

noting that each required member of set of inserted characters could
instead be obtained from a single String, using respectively substring
and charAt.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|

0 new messages