任意货币字符串 - 将所有部分分开?
我有任意字符串,其货币如 100,00€
或 $100.00
或 100.00USD
(任意长度,地球上任何有效的货币,符号和 ISO -代码)...(=如 100.000.000,00 EUR
)。不能保证货币是正确的,它可能是无效的符号或字符或位于错误的位置(数字之后或之前)...
最简单的获取方法是什么:
- 整数部分
- 小数部分
- 货币 (如果有效)
我知道 NumberFormat/CurrencyFormat
但这个类只有在您提前知道确切的语言环境时才有用,并且似乎只适用于格式正确的字符串...asw 只会返回数字,不是货币...
非常感谢! 马库斯
I have arbitrary String with currencies like 100,00€
or $100.00
or 100.00USD
(arbitrary lenght, any valid Currency on earth both Symbol and ISO-Code )...(=like 100.000.000,00 EUR
). There is no guarantee that the currencies are correct, it might be an invalid Symbol or Character or at the wrong position (after or before the number)...
What is the easiest way to get:
- The integer part
- The decimal part
- The Currency (if valid)
I know of NumberFormat/CurrencyFormat
but this class is only usefull if you know the exact locale in advance and seems to be working only to correctly formatted string... asw well only returns the number, not the currency...
Thank you very much!
Markus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了帮助回答这个问题,我们首先应该问,货币串由什么组成?
它由以下部分组成:
Character.isSpaceChar
或Character.isWhitespace
)我很快就会为这个问题创建一个具体的类,但现在我希望这提供了一个开始
给你点。但请注意,正如我在评论中所解释的那样,某些货币符号(例如
$
)无法在没有更多符号的情况下唯一地标识特定货币。编辑:
以防万一其他人访问此页面并遇到同样的问题,我编写了下面的代码来更具体地回答问题。下面的代码属于公共领域。
它返回下面定义的CurrencyValue 对象。
To help answer this question we should first ask, what does a currency string consist of?
Well it consists of:
Character.isSpaceChar
orCharacter.isWhitespace
)I will soon create a concrete class for this question, but for now I hope this provides a starting
point for you. Note, however, that some currency symbols such as
$
cannot uniquely identify a particular currency without more, as I explained in my comment.Edit:
Just in case someone else visits this page and encounters the same problem, I've written the code below that answers the question more concretely. The code below is in the public domain.
It returns a CurrencyValue object defined below.