Is there a way in Javascript to abbreviate currency.
Like if it is $1000 then it convert it into 1K.
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>
Well, I dont know, from where to start?
I cant think of anything.
Can you provide me a starting point.
Sure, it's easy.
function convertCurrency(a)
{
if(a=="$1000")
{
a="1K";
}
return a;
}
window.alert(convertCurrency("$1000")); //alert 1K
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
<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
> 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
> 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, ....|