简体   繁体   English

JavaScript将货币分为数字值和货币符号/名称

[英]JavaScript separate currency into number value and currency symbol/name

I am getting a currency value from a web service, that I would like to display in a number input (the float part) and it's currency symbol/name on a simple label that's next to the input. 我从Web服务获取货币值,该值要显示在数字输入(浮点部分)中,并且是输入旁边的简单标签上的货币符号/名称。

Example of data that I get from the web service: 我从Web服务获取的数据示例:

$ 1.200,05 $ 1.200,05

R$ 1200.05 1200.05雷亚尔

kr. r 1,200.05 1,200.05

37,200.05 kr. 37,200.05韩元。

$300 $ 300

500.0 € 500.0€

You can see that the data is very mixed. 您会看到数据非常混杂。

The symbol/currency name can be before or after the number value, it can have a space between the symbol and the number, or no space at all. 符号/货币名称可以在数字值之前或之后,在符号和数字之间可以有空格,也可以完全没有空格。 It can also have a dot inside the currency name, that I would still like to keep (like with the danish krone: kr. ) 我仍要保留货币名称中的 (例如丹麦克朗: kr)。

The decimal mark can either be a ' . 小数点可以是' ' or a ' , ' and it can be followed by any number of decimals. '或' ',然后可以跟任意数量的小数。 Same with the thousand separator : it can be either a '.' 千位分隔符相同:它可以是“。” or a ',' 或“,”

I have the following snippet to get the number value, but i'm having trouble getting the currency string part: 我有以下代码段获取数字值,但在获取货币字符串部分时遇到了麻烦:

if (!isNaN(cost.charAt(0))) { //check whether it starts with number or string, to adjust the regex
    var regex = /([+-]?[0-9|^.|^,]+)[\.|,]([0-9])/
    var result = regex.exec(cost);
    var floatResult = result? result[1].replace(/[.,]/g, "")+ "." + result[2] : cost.replace(/[^0-9-+]/g, "");
    return floatResult;
}
else {
    var regex = /([+-]?[0-9|^.|^,]+)[\.|,]([0-9]+)$/igm
    var result = regex.exec(cost);
    var floatResult = result? result[1].replace(/[.,]/g, "")+ "." + result[2] : cost.replace(/[^0-9-+]/g, "");
    return floatResult;
}   

I am using jQuery and AngularJS in my webapp, so if there's an easier method with the help of one of those, it would be nice. 我在我的web应用程序中使用jQuery和AngularJS,因此,如果有其中一种方法的帮助,它会更简单。

I'm not sure how to use regex to do this, but what I might do without regex is: 我不确定如何使用正则表达式来执行此操作,但是如果没有正则表达式,我可能会做的是:

a) record the index of the first numeric character a)记录第一个 数字字符的索引

b) record the index of the last numeric character b)记录最后一个数字字符的索引

c) take the substring from the first to last numeric characters and convert that to a Number, Number(numerics) (you will have to remove commas between numbers for this step) c)从第一个数字字符到最后一个数字字符获取子字符串,然后将其转换为数字,即Number(numerics) (为此,您必须删除数字之间的逗号)

d) if the first numeric character index is 0, the currency symbol/name will be at the end of the string, after the last numeric character d)如果第一数字字符索引为0,货币符号/名称将在字符串的末尾,最后的数字字符

e) otherwise it will be at the front of the string, before the first numeric character. e)否则它将在字符串的开头,第一个数字字符之前


edit 编辑

you may be able to simplify the whole thing if you can be certain that what you get back as a response always contains one space that seperates the value and the currency symbol. 如果您可以确定返回的内容始终包含一个将值和货币符号分隔开的空格,则可以简化整个过程。 If that were true (as it is in your examples), you could just use string.split(' ') to get an array of the two strings, and check which one is the number. 如果这是真的(如您的示例中所示),则可以只使用string.split(' ')来获取两个字符串的数组,然后检查哪个是数字。

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

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