0

I have this variable data which contains a huge HTML code.

Somewhere in it, there are <td></td> tags that contains an amount (eg. <td>1000.00</td>). I want to format it to English notation, however, I am not sure how to do it since it is a huge string.

What would be the best approach here?

Leandro Garcia
  • 3,138
  • 11
  • 32
  • 44

5 Answers5

1

I suggest you generate in the data string

<td class="format-this">1000.00</td>

then in jquery,

var MyDataToFormat = $("<div></div>").html(data);

OR

var MyDataToFormat = $("<div />", { html: data }) //thankz for suggesting this

now you will get a DOM object.

then use number formatter Jquery number formatter like this one to format those classes.

$(MyDataToFormat).find('.format-this').each(function() {
    $(this).html($.number($(MyDataToFormat).children('.format-this').html()));
});

This will change the html inside the DOM with ure format. Let me know if this help, haven't test, wrote it raw

sample working, http://jsfiddle.net/q46LC/

WickStargazer
  • 409
  • 2
  • 10
  • Maybe, better $("
    ", { value: data }) then $("
    ").val(data)? Also, further you include already jQuery object into $(MyDataToFormat)
    – Artem Fitiskin Apr 28 '14 at 07:04
  • Sorry mate, i updated the code. Instead of using children, need to use find. Instead of loading with .val, load with .html. Also gave a fiddle sample – WickStargazer Apr 28 '14 at 07:28
0

Could you clarify further? Are you searching specifically for 1000.00 or are you searching for all tags that have some numeric data within them?

You could probably get away with

    $('td').each(function() { //check value here 
    });
Mark Ibanez
  • 657
  • 7
  • 9
0

for converting to currency format use below function. See Demo

var amount = "1000.00";
alert(toUSD(amount));

function toUSD(number) {
    var number = number.toString(), 
    dollars = number.split('.')[0], 
    cents = (number.split('.')[1] || '') +'00';
    dollars = dollars.split('').reverse().join('')
        .replace(/(\d{3}(?!$))/g, '$1,')
        .split('').reverse().join('');
    return '$' + dollars + '.' + cents.slice(0, 2);
}
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
0

Use

parseInt("Your number from html");

Then use this plugin to format. or just use native.

http://www.teamdf.com/web/jquery-number-format/178

Gian Carlo
  • 215
  • 2
  • 6
0

You can use jquery.formatNumber plugin:

jQuery plugin for formatting numbers, good for currencies. Option to choose the cents and thousands digit. Defaults to US' 9,999.00.

Code:

//produces 29,323,894.23 (e.g. US standard)
$('td').formatNumber({
  cents: '.',
  decimal: ','
});

Demo: http://jsfiddle.net/E5rWF/

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111