Solved

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

Posted on 2004-05-03
7
1,694 Views
Last Modified: 2012-05-04
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:

Description

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
0
Comment
Question by:Lee_Nover
  • 5
  • 2
7 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
Comment Utility
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
And it seams you misunderstood what Experts-Exchange is for.
It is surely not for you to find nerds doing your job :-)
0
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
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 :)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You can test your format at this link:
http://jan.kollhof.net/projects/js/jsolait/jsolaitonline.xhtml

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

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Small side note as hint: using words as asap lets people remember to their office's boss :-)
0
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
I'll add the %n format as that's what I actually need
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
Thanks for the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now