Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The round(num, decimalPlaces) function doesn't work on locales that use comma as the decimal separator. #64

Closed
mdudzinski opened this issue Jun 6, 2017 · 0 comments

Comments

@mdudzinski
Copy link
Contributor

Software versions

JavaRosa since 7d0e596 (PR #33 , Issue #5).

Problem description

The round(num, decimalPlaces) function doesn't work on locales that use comma as the decimal mark (Polish Locale for example:)).

Steps to reproduce the problem

I reproduced this problem by running the test suite on my machine which has PL locale.
The suite crashes with the following information:

java.lang.NumberFormatException: For input string: "14,3"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at org.javarosa.xpath.expr.XPathFuncExpr.round(XPathFuncExpr.java:1055)
at org.javarosa.xpath.expr.XPathFuncExpr.eval(XPathFuncExpr.java:200)
at org.javarosa.xpath.test.XPathEvalTest.testEval(XPathEvalTest.java:86)
at org.javarosa.xpath.test.XPathEvalTest.testEval(XPathEvalTest.java:65)
at org.javarosa.xpath.test.XPathEvalTest.doTests(XPathEvalTest.java:322)

This comes from test for "round('14.29123456789', 1)" expression but I checked and the suite will fall for any round expression that have more than 0 as the power parameter.

Expected behavior

The test just passes.

Other information

The following code causes this behaviour:

            final NumberFormat nf = NumberFormat.getNumberInstance();
            nf.setMaximumFractionDigits(numDecimals);
            nf.setGroupingUsed(false);
            return Double.valueOf(nf.format(number));

It uses NumberFormat's capabilities to round a decimal number. It works great but this class follows the current locale settings and produces a readable string that matches the locale's rules. The result is later passed to Double.valueOf which doesn't handle comma as the decimal separator.

For example, in Poland nf.format(number) for number=14.29123456789 and numDecimals=1 will return 14,3 instead of 14.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant