Abbreviate Currency using Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sunny

    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.
  • Thomas 'PointedEars' Lahn

    #2
    Re: Abbreviate Currency using Javascript

    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 300dec7@news.de mon.co.uk>

    Comment

    • Sunny

      #3
      Re: Abbreviate Currency using Javascript

      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.

      Comment

      • Stevo

        #4
        Re: Abbreviate Currency using Javascript

        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

        Comment

        • Conrad Lender

          #5
          Re: Abbreviate Currency using Javascript

          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

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Abbreviate Currency using Javascript

            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

            Comment

            • Conrad Lender

              #7
              Re: Abbreviate Currency using Javascript

              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

              Comment

              • Dr J R Stockton

                #8
                Re: Abbreviate Currency using Javascript

                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, ....|

                Comment

                Working...