Solved

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

Posted on 2004-05-03
7
1,774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

630 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