Solved

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

Posted on 2004-05-03
7
1,724 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

778 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