简体   繁体   English

Javascript对货币字符串进行排序

[英]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. 我对JavaScript还是很陌生,并且想知道如何创建语法来将字符串格式($ 1,234.56)的货币值转换为可以排序的数字(1234.56)。 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 //用于javascript正则表达式

\\$ for $ \\$$

^\\$ for $ at the begin ^\\$$的开始

, for , ,,

| for OR operator 对于OR运算符

g for search all match g用于搜索所有匹配项

Hope this help. 希望能有所帮助。

Check the library jquery-maskMoney which handles all of this for you. 检查库jquery-maskMoney ,它为您处理了所有这一切。

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 并且如果yourNumberInput曾说过,值1999.99它将变成$ 1.999,99

You can also transform your input back and configure various formatting settings. 您还可以将输入转换回去并配置各种格式设置。 (Note you need jquery in order to use it) (请注意,您需要使用jquery才能使用它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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