Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6691047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:42:24+00:00 2026-05-26T05:42:24+00:00

UPDATED: Using javascript or jQuery, how can I convert a number into it’s different

  • 0

UPDATED:

Using javascript or jQuery, how can I convert a number into it’s different variations:

eg:

1000000
to…

1,000,000 or 1000K

OR

1000
to…

1,000 or 1K

OR

1934 and 1234
to…

1,934 or -2K (under 2000 but over 1500)

or

1,234 or 1k+  (over 1000 but under 1500)

Can this is done in a function?

Hope this make sense.

C

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:42:24+00:00Added an answer on May 26, 2026 at 5:42 am

    You can add methods to Number.prototype, so for example:

    Number.prototype.addCommas = function () {
        var intPart = Math.round(this).toString();
        var decimalPart = (this - Math.round(this)).toString();
        // Remove the "0." if it exists
        if (decimalPart.length > 2) {
            decimalPart = decimalPart.substring(2);
        } else {
            // Otherwise remove it altogether
            decimalPart = '';
        }
        // Work through the digits three at a time
        var i = intPart.length - 3;
        while (i > 0) {
            intPart = intPart.substring(0, i) + ',' + intPart.substring(i);
            i = i - 3;
        }
        return intPart + decimalPart;
    };
    

    Now you can call this as var num = 1000; num.addCommas() and it will return "1,000". That’s just an example, but you’ll find that all the functions create will involve converting the numbers to strings early in the process then processing and returning the strings. (The separating integer and decimal part will probably be particularly useful so you might want to refactor that out into its own method.) Hopefully this is enough to get you started.

    Edit: Here’s how to do the K thing… this one’s a bit simpler:

    Number.prototype.k = function () {
        // We don't want any thousands processing if the number is less than 1000.
        if (this < 1000) {
            // edit 2 May 2013: make sure it's a string for consistency
            return this.toString();
        }
        // Round to 100s first so that we can get the decimal point in there
        // then divide by 10 for thousands
        var thousands = Math.round(this / 100) / 10;
        // Now convert it to a string and add the k
        return thousands.toString() + 'K';
    };
    

    Call this in the same way: var num = 2000; num.k()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code in javascript and I am using jQuery Calculation plugin (can
I'm using the following to grab some updated HTML and insert it into a
I'm having trouble with updating an ASP:UpdatePanel using javascript (jQuery). Here're what I have.
you know send a ajax you can use javascript lib like: jquery $.ajax or
(Note: I'm using jQuery below, but the question is really a general JavaScript one.)
I need to redirect to a page using javascript/jquery. I have a page with
UPDATED Using RedDot CMS, linking to a 'sub-page' (page within a page) directly will
I have a ticker which items are updated using polling. I have written a
UPDATED: I am using MySQL statement to create a view: I need to show
( Updated a little ) I'm not very experienced with internationalization using PHP, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.