0

I want to use integer %00d in javascript using jquery. like that integer 8 will print output like 008.

In C printf("%00d", 8); How about javascript?

How to solve it problem? Thanks any reply.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
Steve Austin
  • 103
  • 1
  • 1
  • 8

2 Answers2

1

This could help.

('000'+8).substr(-3)
Thalaivar
  • 23,282
  • 5
  • 60
  • 71
0

If you'd like a library, you should give sprintf a try.

If not, there are several interesting options in this question in SO. Particularly, I liked this solution:

// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}
"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")
Community
  • 1
  • 1
raviolicode
  • 2,155
  • 21
  • 22