3

So here's my problem: I've got a price that I'm pulling via AJAX, but what I'm pulling from doesn't add the last zero to the price if it ends in a zero. For example, something that is .40 cents will come back as .4. So now I'm trying to find a way to target the div where the price is stored, split the price at the period, and if the length of the cents column is less than 2, add a zero to the price. That's the code I need. Here's the code I have.

var split4 =  $('#price selling4').text();
var arr4 = split4.split('.');
if (arr4[2] < 2) {
    $("#price selling4").append("0")
}

I said it before and I'll say it again: I'm not the brightest programmer. But from my googling this seems to be what I'm looking for. However, whenever plugged in to console it does nothing. The append by itself doesn't even work. And the variables come back as undefined. Not really sure what else I can do with this by myself.

user2985714
  • 81
  • 1
  • 7
  • Can you show the HTML of the div? `#price selling4` would mean a `` tag within a container whose id was `price`; that's almost certainly not what you mean. – Paul Roub Nov 14 '13 at 22:33
  • is what I have. I didn't think the space would hurt anything. I'll try it without. – user2985714 Nov 14 '13 at 22:34
  • ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). Per [this answer](http://stackoverflow.com/a/1077111/17300) -- so your ID is not allowed to contain spaces. – Stephen P Nov 15 '13 at 01:09

5 Answers5

1

Try:

$("#price selling4").text( ($("#price selling4").text()*1).toFixed(2) );

This formats the number using fixed-point notation - simply using the toFixed method:

Your id price selling4 looks pretty funky too - spaces?

Though it would be best to do this with the returned ajax data before you add it to the $("#price selling4") element.

iambriansreed
  • 21,935
  • 6
  • 63
  • 79
1

You want to get the number, and make it use toFixed(2).

$('#price selling4').text(+($('#price selling4').text()).toFixed(2));

should be all you need.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
0

The actual problem is your logic, should be arr4[1], and you should be checking the value is less than 10.

var split4 =  $('#price').text();
var arr4 = split4.split('.');
console.log(arr4)
if (arr4.length == 2 && arr4[1] < 10) {
    $("#price").append("0")
}

http://jsfiddle.net/YGudN/

Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
0

your div ID isn't legal according to HTML 4.01 specification. You're getting undefined error because the var split4 is not being assigned with anything.

Try to rename your div to one single word, should be enough.

0

I'd suggest:

$("#price selling4").text(function(i,t){
    return parseFloat(t).toFixed(2);
})

Incidentally, note that you seem to have a white-space in your id (which is invalid) or you're looking for an element <selling4> (which doesn't exist). If the selling4 is a class-name, please prefix (as with CSS) with a period, to give .selling4.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410