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

  • Home
  • SEARCH
  • 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 6821655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:34:13+00:00 2026-05-26T21:34:13+00:00

I want to format a number to have two digits. The problem is caused

  • 0

I want to format a number to have two digits. The problem is caused when 0–9 is passed, so I need it to be formatted to 00–09.

Is there a number formatter in JavaScript?

  • 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-26T21:34:14+00:00Added an answer on May 26, 2026 at 9:34 pm

    Edit (2021):

    It’s no longer necessary to format numbers by hand like this anymore. This answer was written way-back-when in the distant year of 2011 when IE was important and babel and bundlers were just a wonderful, hopeful dream.

    I think it would be a mistake to delete this answer; however in case you find yourself here, I would like to kindly direct your attention to the second highest voted answer to this question as of this edit.

    It will introduce you to the use of .toLocaleString() with the options parameter of {minimumIntegerDigits: 2}. Exciting stuff. Below I’ve recreated all three examples from my original answer using this method for your convenience.

    [7, 7.5, -7.2345].forEach(myNumber => {
      let formattedNumber = myNumber.toLocaleString('en-US', {
        minimumIntegerDigits: 2,
        useGrouping: false
      })
      console.log(
        'Input:    ' + myNumber + '\n' +
        'Output:   ' + formattedNumber
      )
    })

    Original Answer:

    The best method I’ve found is something like the following:

    (Note that this simple version only works for positive integers)

    var myNumber = 7;
    var formattedNumber = ("0" + myNumber).slice(-2);
    console.log(formattedNumber);

    For decimals, you could use this code (it’s a bit sloppy though).

    var myNumber = 7.5;
    var dec = myNumber - Math.floor(myNumber);
    myNumber = myNumber - dec;
    var formattedNumber = ("0" + myNumber).slice(-2) + dec.toString().substr(1);
    console.log(formattedNumber);

    Lastly, if you’re having to deal with the possibility of negative numbers, it’s best to store the sign, apply the formatting to the absolute value of the number, and reapply the sign after the fact. Note that this method doesn’t restrict the number to 2 total digits. Instead it only restricts the number to the left of the decimal (the integer part). (The line that determines the sign was found here).

    var myNumber = -7.2345;
    var sign = myNumber?myNumber<0?-1:1:0;
    myNumber = myNumber * sign + ''; // poor man's absolute value
    var dec = myNumber.match(/\.\d+$/);
    var int = myNumber.match(/^[^\.]+/);
    
    var formattedNumber = (sign < 0 ? '-' : '') + ("0" + int).slice(-2) + (dec !== null ? dec : '');
    console.log(formattedNumber);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to format a number as a percent, with at least 2 digits
I have two fields in my text file which are timestamp number The format
I have the need to format a decimal number as currency but I do
I want to format a number in indian format. for example, x= 123456 should
I want to know how can i format a number like 2000000 into standard
I want to call a number in below format using platformRequest: platformRequest(tel:*123*33584744#); But it
I want to make a program that will read some number in string format
When a user goes to edit a number, I want to format a number
I have a variable line_number_max_digits and I want to use it in string format
I'm xml-serializing a object with a large number of properties and I have two

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.