简体   繁体   中英

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?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM