Abbreviate Currency using Javascript

S

Sunny

Hi,

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

Thomas 'PointedEars' Lahn

Sunny said:
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
 
S

Sunny

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, <[email protected]>

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

Stevo

Sunny said:
Hi,

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;
}

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

Thomas 'PointedEars' Lahn

Sunny said:
Thomas said:
Sunny said:
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
 
D

Dr J R Stockton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top