Dys
Dys

Reputation: 23

Javascript Sort for Currency String

I'm quite new to JavaScript and would to know how to create syntax that will convert currency values that are in string format ($1,234.56) to numbers (1234.56) that can be sorted. Or is there perhaps a way of just sorting the values without removing the string format?

Upvotes: 1

Views: 4187

Answers (2)

JSelser
JSelser

Reputation: 3630

Check the library jquery-maskMoney which handles all of this for you.

It let's you do stuff like:

$('#yourNumberInput').maskMoney()

and if yourNumberInput had say, the value 1999.99 it would turn it into $ 1.999,99

You can also transform your input back and configure various formatting settings. (Note you need jquery in order to use it)

Upvotes: -1

hungndv
hungndv

Reputation: 2141

Here you are:

var yourNumber = Number('$1,234.56'.replace(/(^\$|,)/g,''));
alert(yourNumber + 1);

// for javascript Regex

\$ for $

^\$ for $ at the begin

, for ,

| for OR operator

g for search all match

Hope this help.

Upvotes: 2

Related Questions