473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Abbreviate Currency using Javascript

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
7 3082
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar...
3
by: Dave Stone | last post by:
This question appeared years ago in the context of Acc2K and SQL Server 7, but no replies were posted. HOWEVER!! It still seems to be a problem with Acc XP and SQL Server 2000. Surely someone has a...
11
by: Adrian | last post by:
Hi I want to use the following declarations but vb dotnet keeps complaining that currency can't be used because it private ? I have tried it in a module and in the declaration pare same error!...
10
by: jayender.vs | last post by:
Hello guys, I need to know the Currency conversion code in Javascript Say for example i got 2 textbox .. where i enter a number (singapore doller value) and i provide a button and in the next text...
16
by: xjohnx | last post by:
Hi, I'm hoping someone can help me I am quite new to Javascript and have had to create a programme which converts dollars into euros and vice versa, here is my script which is working var...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
1
by: jereesh | last post by:
Hi, I need to display formatted currency values in the My UI currency fields. I would like to know how to do currency formating in javascript . ie when I pass the value and the currency code it...
16
by: Alexio | last post by:
Am new to javascript. I need to format the currency in the as numbers are entered in textboxes. I can format the currency in the calculations and display in the totals but can not figure out how to...
10
by: Chris H | last post by:
Greetings, I'm trying to update an address field with "standard" abbreviations so that I can do a comparison of various accounts to one another on the address. I can update a set of records for...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.