format string in javascript

11,579

Read this article to use javascript similary sprintf in C...

http://www.webtoolkit.info/javascript-sprintf.html

or better read: JavaScript equivalent to printf/string.format

Share:
11,579
n0minal
Author by

n0minal

Updated on June 04, 2022

Comments

  • n0minal
    n0minal almost 2 years

    Is it possible to achieve this in javascript?

    Here is the format:

    1 ITEM1      9.00   0%  9.00
    1 ITEM-GET01 8.00   12% 5.00
    

    I would like to create a receipt like layout. I'm using backbone.js so there will be models and collection involve.

    Also if it were on a table can I use jquery to get the data from the table tr then have the result just like what is posted above?

    I was able to read about sprintf but I don't think it it is the one I need. any ideas?

    UPDATE

    I'm trying out sprintf here is what i've come so far

    var result = sprintf("%d %-s %.2f %d%% %.2f", model.get("Qty"), model.get("Itemname"), model.get("Price"), model.get("Discount"), model.get("ExtPrice"));
    

    result is:

    1 Item1 1.49 0% 1.49