- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在欧洲,小数点用“,”分隔,我们使用可选的“.”来分隔千位。我允许以下货币值:
我使用下一个正则表达式(来自 RegexBuddy 库)来验证输入。我允许可选的两位数分数和可选的千位分隔符。
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\.[0-9]{0,2})?|(?:\.[0-9]{3})*(?:,[0-9]{0,2})?)$
我想将货币字符串解析为 float 。例如
123,456.78 应该存储为 123456.78
123.456,78 应存储为 123456.78
123.45 应存储为 123.45
1.234 应存储为 123412.34应该存储为12.34
等等……
在 Java 中有没有简单的方法来做到这一点?
public float currencyToFloat(String currency) {
// transform and return as float
}
使用 BigDecimal 而不是 Float
感谢大家的精彩回答。我已将代码更改为使用 BigDecimal 而不是 float。我会将这个问题的前一部分保留为 float ,以防止人们犯我将要犯的同样错误。
解决方案
下一个代码显示了一个函数,该函数将美国和欧盟货币转换为 BigDecimal(String) 构造函数接受的字符串。这就是说一个没有千位分隔符的字符串和一个小数点。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestUSAndEUCurrency {
public static void main(String[] args) throws Exception {
test("123,456.78","123456.78");
test("123.456,78","123456.78");
test("123.45","123.45");
test("1.234","1234");
test("12","12");
test("12.1","12.1");
test("1.13","1.13");
test("1.1","1.1");
test("1,2","1.2");
test("1","1");
}
public static void test(String value, String expected_output) throws Exception {
String output = currencyToBigDecimalFormat(value);
if(!output.equals(expected_output)) {
System.out.println("ERROR expected: " + expected_output + " output " + output);
}
}
public static String currencyToBigDecimalFormat(String currency) throws Exception {
if(!doesMatch(currency,"^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{0,2})?|(?:,[0-9]{3})*(?:\\.[0-9]{0,2})?|(?:\\.[0-9]{3})*(?:,[0-9]{0,2})?)$"))
throw new Exception("Currency in wrong format " + currency);
// Replace all dots with commas
currency = currency.replaceAll("\\.", ",");
// If fractions exist, the separator must be a .
if(currency.length()>=3) {
char[] chars = currency.toCharArray();
if(chars[chars.length-2] == ',') {
chars[chars.length-2] = '.';
} else if(chars[chars.length-3] == ',') {
chars[chars.length-3] = '.';
}
currency = new String(chars);
}
// Remove all commas
return currency.replaceAll(",", "");
}
public static boolean doesMatch(String s, String pattern) {
try {
Pattern patt = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = patt.matcher(s);
return matcher.matches();
} catch (RuntimeException e) {
return false;
}
}
}
最佳答案
回答一个稍微不同的问题:不要使用浮点类型来表示货币值。 It will bite you。请改用以 10 为基数的类型,例如 BigDecimal
,或整数类型,例如 int
或 long
(代表您的值的数量 - 便士,例如,以美元计算)。
您将无法存储精确值 - 例如 123.45,作为 float ,对该值的数学运算(例如乘以税率)会产生舍入误差。
该页面的示例:
float a = 8250325.12f;
float b = 4321456.31f;
float c = a + b;
System.out.println(NumberFormat.getCurrencyInstance().format(c));
// prints $12,571,782.00 (wrong)
BigDecimal a1 = new BigDecimal("8250325.12");
BigDecimal b1 = new BigDecimal("4321456.31");
BigDecimal c1 = a1.add(b1);
System.out.println(NumberFormat.getCurrencyInstance().format(c1));
// prints $12,571,781.43 (right)
在涉及金钱时,您不想犯错误。
关于最初的问题,我有一段时间没有接触 Java,但我知道我想远离 regex 来做这种工作。我看到这个推荐;它可能对你有帮助。未经测试;警告开发者。
try {
String string = NumberFormat.getCurrencyInstance(Locale.GERMANY)
.format(123.45);
Number number = NumberFormat.getCurrencyInstance(locale)
.parse("$123.45");
// 123.45
if (number instanceof Long) {
// Long value
} else {
// too large for long - may want to handle as error
}
} catch (ParseException e) {
// handle
}
寻找规则符合您期望看到的区域设置。如果找不到,请按顺序使用多个,或创建您自己的 custom NumberFormat。
我还会考虑强制用户以单一的规范格式输入值。 123.45 和 123.456 看起来方式对我来说太相似了,按照你的规则会导致值相差 1000 倍。This is how millions are lost。
关于java - 如何将货币金额(美国或欧盟)解析为 Java 中的浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/965831/
条件: 我们需要我们 Magento 网站的默认显示货币是美元,Paypal(基础货币)也需要是美元(因为我们需要客户以美元支付)。 我们的 Fedex a/c 是在新加坡开设的,结果发现运费是以新加
根据 currency_str 和 created_date_time 合并两个数据帧(xrate 和 df)时遇到问题 display(xrate.info()) Int64Index: 1611
我必须匹配像 这样的值 € 6.483,00 或类似的值 18,50% 或者,再次, +65,86 % 在我起草的 Javascript 函数中: function(s) { return /^
我正在尝试将货币金额解析并存储为 BigDecimal 值。我得到了相关货币的区域设置,在大多数情况下它工作正常,但当货币是哥斯达黎加科朗时我得到了意想不到的结果。 我的哥斯达黎加客户告诉我,典型的货
在当前版本的 Excel(Office 365/2019 年 2 月)中获得近似每日外汇汇率的最简单的免费方法是什么。 我对历史数据不感兴趣,我只想要从货币 X 到货币 Y 的最后已知日汇率,并且只在
我在 spree 时遇到了麻烦,不知道如何处理。 我无法更改主要货币。 我该怎么做? 最佳答案 它在 Spree 2.0.0 中更容易,而且显然它也可以在以前的 spree 版本中工作。 转到您的 c
在我的网络应用程序中,我有一个名为“预算”的输入字段,用户可以在其中输入项目的建议预算。我需要创建一个屏蔽输入,以便在用户在输入字段中输入时自动将输入的金额转换为以下格式: 1000 10 000 1
我正在从数据库返回一个字符串,但由于数据库的编码方式,一些英镑符号 (£) 被问号取代。我想恢复井号,但不替换字符串中真正的问号。我已经设法编写了一个正则表达式来测试问号后跟数字的组合,但我不确定如何
我在使用亚马逊销售 API 时遇到问题。我在 amazon.co.uk 有一个帐户,可以正常发送产品。我在 amazon.de 有一个新帐户,除了货币外,一切都很好。 我有一个零售价为 10 英镑 (
Pharo 有什么方法可以将数字转换为单词。 例如:1200 = 一千二百而已。 实现起来并不难,只是想知道。 最佳答案 Integer>>asWords会这样做。 1200 asWords返回 'o
我有一个简单的单页网络应用程序。它从两个不同的 API 获取货币数据(以美元为单位),在成功检索后,promise 被解决,一些计算得出 GBP/Bitcoin 汇率。 我正试图找到一种干净的方法,然
我有以下简单的计算,它将两个值加在一起。这些值与使用“R”作为前缀标识的兰特(南非货币)有关。 function calculate() { var A = parseFloat(docume
使用 JavaScript,什么是将点替换为逗号的正确方法(对于欧盟货币),例如: 2000.65 将是 2000,65 而不是 2,000.65 39.20 将是 39,20 我不确定 cost.r
我在文本框中显示带有 NumberFormat 的货币符号 NumberFormat numberFormat = NumberFormat.getSimpleCurrencyFo
我已经设法从非接触式阅读器读取了一个交易事件,使用 现在我的 Activity 打开了,我被困在那个点上,因为我认为我的 Intent 中会有一些数据,比如 amou
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
这个问题在这里已经有了答案: Why not use Double or Float to represent currency? (16 个答案) 关闭 3 年前。 我的应用程序中的一些计算有问题
我使用 Jruby(反正就是 ruby,在 jvm 下运行 :D )和马拉松测试(一个 java swing 应用程序),我在处理货币数字时遇到了一些麻烦。 我不使用 Rails(不知道我是否可以
我正在尝试删除 、 或 之后的尾随零。 以一种货币表示,仍然保留货币符号。例如,€90.00 到 €90、90.00€ 到 90€ 和 €90.33 到 €90.33。 示例如下: $('.produ
我想将带小数点的数字(以货币计)转换成单词 例如:12345.60--> 一万二千三百四十五美元六十美分 我从 http://www.csharp-tutorials.info/2016/04/con
我是一名优秀的程序员,十分优秀!