Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing dollar signs from prices

I'm building a string of amounts but need to remove the dollar signs. I have this jQuery code:

  buildList($('.productPriceID > .productitemcell'), 'pricelist')

It's returning

pricelist=$15.00,$19.50,$29.50

I need to remove the dollar signs but can't seem to figure it out. Tried using .trim but I think that removes only white space.

Sorry for the newbie question! Thanks in advance for any help!

Here's the full code:

function buildList(items, name) {
var values = [];
items.each(function() {
values.push(this.value || $(this).text());
});
return name + '=' + values.join(',');
}

var result = [
buildList($('.productCodeID > .productitemcell'), 'skulist'),
buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

var string = result.join('&');

Here is the raw code before the javascript runs

<span class="productPriceID">
<div class="productitemcell">$15.00</div>
<div class="productitemcell">$19.50</div>
<div class="productitemcell">$29.50</div>
</span>
like image 386
Mike Muller Avatar asked Nov 17 '10 21:11

Mike Muller


People also ask

How do I get rid of dollar sign?

To get rid of the dollar sign, first select the cells with dollar signs you want to remove. Then, in the Toolbar, click on Format > Number > Number. As a result, the dollar sign is deleted from the selected cells.

How do I get rid of the dollar sign in Excel?

In the Data Type Format dialog box, do one of the following: To add a currency symbol, click Currency, and then in the Currency list, select the type of currency that you want to display. To remove a currency symbol, click Number.

How do I remove the dollar sign from a data set in R?

Dollar signs can also be removed from a dataframe column or row, by using the gsub() method. All the instances of the $ sign are removed from the entries contained within the data frame.

How do I get rid of the dollar sign in Python?

replace('$','') method on the entire column. This is the most straightforward method, as it simply replaces the '$' with a blank space for each item in the column.


1 Answers

EDIT: Starting over with answer now that I have the code that is running.

Looking at your updated code, this should work:

Example: http://jsbin.com/ekege3/

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

result[ 2 ] = result[ 2 ].replace(/\$/g, '');

var string = result.join('&');

Side note: You can shorten your buildList function a little like this:

function buildList(items, name) {
    return (name + '=') + items.map(function() {
        return (this.value || $(this).text());
    }).get().join(',');
}

Original answer:

If you have a string, just use .replace().

var str = "pricelist=$15.00,$19.50,$29.50";

str = str.replace(/\$/g, '');

Or are you saying that you have a variable pricelist containing an Array? If so, do this:

var pricelist = ["$15.00","$19.50","$29.50"];

for( var i = 0, len = pricelist.length; i < len; i++ ) {
    pricelist[ i ] = pricelist[ i ].replace('$', '');
}

EDIT: It sounds as though the buildList method returns an Array.

One way to check would be to do this:

alert( Object.prototype.toString.call( result[2] ) );

And see what it gives you.

Anyway, assuming it's an Array, here's the updated version of the second example.

var result = [
    buildList($('.productCodeID > .productitemcell'), 'skulist'),
    buildList($('.productQuantityID > .productitemcell > input'), 'quantitylist'),
    buildList($('.productPriceID > .productitemcell'), 'pricelist')
];

// verify the data type
alert( Object.prototype.toString.call( result[ 2 ] ) );

// loop over result[ 2 ], replacing the $ with ''
for( var i = 0, len = result[ 2 ].length; i < len; i++ ) {
    result[ 2 ][ i ] = result[ 2 ][ i ].replace('$', '');
}

var string = result.join('&');

like image 161
user113716 Avatar answered Oct 13 '22 20:10

user113716