Format (delphi) or sprintf (c/c++) equivalent in JS

I'd prefer the delphi syntax :)

it would look like:
function Format(s, args) {
// s - the string to be formatted
// args - string format arguments

indexed arguments are not mandatory but a nice feature
precision speficifiers are needed

this is from the Delphi help:


Format strings passed to the string formatting routines contain two types of objects -- literal characters and format specifiers. Literal characters are copied verbatim to the resulting string. Format specifiers fetch arguments from the argument list and apply formatting to them.

Format specifiers have the following form:

"%" [index ":"] ["-"] [width] ["." prec] type

A format specifier begins with a % character. After the % come the following, in this order:

An optional argument zero-offset index specifier (that is, the first item has index 0), [index ":"]
      An optional left justification indicator, ["-"]
      An optional width specifier, [width]
      An optional precision specifier, ["." prec]
      The conversion type character, type

I need the following types:

d      Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros.

f      Fixed. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative.The number of digits after the decimal point is given by the precision specifier in the format string—a default of 2 decimal digits is assumed if no precision specifier is present.

n      Number. The argument must be a floating-point value. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format corresponds to the "f" format, except that the resulting string contains thousand separators.

s      String. The argument must be a character, a string, or a PChar value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated.

an example:
Format('Available points: %d with an %s grade', [500, 'A'])  would result in : 'Available points: 500 with an A grade'
Format('Now only %n (%d%% off)', [4999.95, 10])  would result in : 'Now only 4.999,95 (10% off)'

for the thousand and decimal symbols I'll have a _global_ variable

should be fairly easy with RegEx to someone who's good at it :)

did I mention I need this asap :P
LVL 12
Who is Participating?
ZvonkoSystems architectCommented:
ZvonkoSystems architectCommented:
And it seams you misunderstood what Experts-Exchange is for.
It is surely not for you to find nerds doing your job :-)
Lee_NoverAuthor Commented:
lol :-)

I would've done it myself but I have no time .. not even to search for it on the web :-/
"JavaScript o lait" looks promising .. I'll check it out
.. and tnx :)
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:
You can test your format at this link:

Enter this for test and press the Run button:
'Available points: %d with an %s grade'.format(500, 'A')

ZvonkoSystems architectCommented:
Small side note as hint: using words as asap lets people remember to their office's boss :-)
Lee_NoverAuthor Commented:
I'll add the %n format as that's what I actually need
ZvonkoSystems architectCommented:
Thanks for the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.