By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Abbreviate Currency using Javascript

P: n/a
Hi,

Is there a way in Javascript to abbreviate currency.
Like if it is $1000 then it convert it into 1K.
Sep 26 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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, <f8*******************@news.demon.co.uk>
Sep 26 '08 #2

P: n/a
On Sep 26, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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?

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$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.
Sep 26 '08 #3

P: n/a
Sunny wrote:
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
Sep 26 '08 #4

P: n/a
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
Sep 26 '08 #5

P: n/a
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
Sep 26 '08 #6

P: n/a
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
Sep 26 '08 #7

P: n/a
On Sep 26, 6:50*pm, Sunny <sunnyluth...@gmail.comwrote:
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, ....|
Sep 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.