473,767 Members | 6,052 Online
Bytes | Software Development & Data Engineering Community
+ 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 3110
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.de mon.co.uk>
Sep 26 '08 #2
On Sep 26, 3:37 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.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$8 300d...@news.de mon.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(co nvertCurrency(" $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.mozil la.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...@we b.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...@g mail.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 = "$100000000 0"
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.""""""""@ph ysics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demo n.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
574
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 multiply(by int)& show, have a constructor w/ & w/out arguments. Header file should have private data & all 6 functions from above.Class definition file should implement my ADT class. What I have so far: Main program #include "jahcurrency.h" #include...
3
2040
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 fix 6 years down the line... I used the Upsizing Wizard to build a SQL database from an Access back-end and link it to the Access f/e. All the data appear to have gone across OK as viewed from Enterprise Manager, but from the front-end all...
11
2957
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! Private Declare Function QueryPerformanceFrequency Lib "kernel32" (ByVal
10
3892
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 box i should get the value in US doller value. Waiting for ur response, Ciao, Jay
16
9528
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 currency; var amountEntered; currency = window.prompt('Please enter 0 to convert from Dollars to euros and 1 to convert form euros to dollars',''); currency = parseFloat(currency); while (currency < 0 || currency > 1) { currency =...
25
5951
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 calculator- looking interface. I'm quite new to C++, and I would need some help. Please any suggestion is welcome :) The following is what I have been able to do so far:
1
1153
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 should display the formatted currency in the field with currency symbol. the Code will be like USD, EURO etc. but when it get displayed it should display the curresponding sysmbol can any body help me in this Regard Regards
16
12089
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 format in input textboxes. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html;...
10
3576
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 "Road" to "Rd", but when I tried to stack the update clauses, I seem to get random updates within the file. All the updates are correct, but they're incomplete. Not sure how this needs to be done, I added a TOP statement but that didn't work. ...
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10171
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10015
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9842
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7384
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3931
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3534
muto222
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.