Solved

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

Posted on 2004-05-03
7
1,710 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
ID: 10977692
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10977713
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
ID: 10977791
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 63

Expert Comment

by:Zvonko
ID: 10977812
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
ID: 10977824
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
ID: 10978057
I'll add the %n format as that's what I actually need
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 11023197
Thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

919 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

19 Experts available now in Live!

Get 1:1 Help Now