Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into 'master'
Browse files Browse the repository at this point in the history
JAFFA-718. Fixed the decimal parse issue for Norwegian Bokmal and other European…

See merge request jaffa/jaffa-framework!266
  • Loading branch information
Pavitra Mohan Bagirthi committed Jan 26, 2021
2 parents 24a85b6 + 5d9ad36 commit 43f92cc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions jaffa-core/source/java/org/jaffa/datatypes/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,21 @@ public static Double parseDecimal(String input, String layout) throws FormatDeci
if (layout == null)
layout = DecimalFieldMetaData.getDecimalFormat();
layout = LocaleHelper.determineLayout(layout);

NumberFormat fmt;
if (LocaleContext.getLocale() != null)
fmt = NumberFormat.getNumberInstance(LocaleContext.getLocale());
else
fmt = NumberFormat.getNumberInstance();
if (layout != null && fmt instanceof DecimalFormat)
if (layout != null && layout.trim().length() > 0 && fmt instanceof DecimalFormat)
((DecimalFormat) fmt).applyPattern(layout);

//If the input string is having decimal point as ".", then it will format the string based on the user locale,
//This is required when the locale is Norwegian and European language which use the comma as decimal separator.
//If we don't use the correct format before parsing, then the input will get parsed wrongly.
if(input.contains(".")){
input = Formatter.format(Double.parseDouble(input));
}
out = new Double(fmt.parse(input).doubleValue());
}
return out;
Expand Down
51 changes: 51 additions & 0 deletions jaffa-core/src/test/java/org/jaffa/datatypes/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.jaffa.datatypes;


import org.jaffa.datatypes.exceptions.FormatDecimalException;
import org.jaffa.presentation.portlet.session.LocaleContext;
import org.junit.Assert;
import org.junit.Test;

import java.util.Locale;

public class ParserTest {

//Test Locale English parse
@Test
public void test_ParseDecimal_EN() {
Double aDouble = null;
try {
LocaleContext.setLocale(new Locale("en", "US"));
aDouble = Parser.parseDecimal("1000.0", null);
Assert.assertEquals(1000.0, aDouble.doubleValue(), 0.00);
} catch (FormatDecimalException e) {
Assert.fail(e.getMessage());
}
}

//Test Locale Norwegian Bokmal parse
@Test
public void test_ParseDecimal_NB() {
Double aDouble = null;
try {
LocaleContext.setLocale(new Locale("nb", "NO"));
aDouble = Parser.parseDecimal("100000.0", null);
Assert.assertEquals(100000.0, aDouble.doubleValue(), 0.00);
} catch (FormatDecimalException e) {
Assert.fail(e.getMessage());
}
}

//Test Locale Arabic parse
@Test
public void test_ParseDecimal_AR() {
Double aDouble = null;
try {
LocaleContext.setLocale(new Locale("ar", "OM"));
aDouble = Parser.parseDecimal("1000.0", null);
Assert.assertEquals(1000.0, aDouble.doubleValue(), 0.00);
} catch (FormatDecimalException e) {
Assert.fail(e.getMessage());
}
}
}

0 comments on commit 43f92cc

Please sign in to comment.