ASF Bugzilla – Attachment 34903 Details for
Bug 60040
CellNumberFormatter wrong formating for Locale pt_BR
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
ProposedSolution+UnitTest
CustomFormatting.patch (text/plain), 7.20 KB, created by
samson
on 2017-04-10 09:06:17 UTC
(
hide
)
Description:
ProposedSolution+UnitTest
Filename:
MIME Type:
Creator:
samson
Created:
2017-04-10 09:06:17 UTC
Size:
7.20 KB
patch
obsolete
>Index: src/java/org/apache/poi/ss/format/CellNumberFormatter.java >=================================================================== >--- src/java/org/apache/poi/ss/format/CellNumberFormatter.java (revision 295302) >+++ src/java/org/apache/poi/ss/format/CellNumberFormatter.java (working copy) >@@ -61,8 +61,10 @@ > private final String denominatorFmt; > private final boolean improperFraction; > private final DecimalFormat decimalFmt; >+ private DecimalFormatSymbols dfs; > >- // The CellNumberFormatter.simpleValue() method uses the SIMPLE_NUMBER >+ >+ // The CellNumberFormatter.simpleValue() method uses the SIMPLE_NUMBER > // CellFormatter defined here. The CellFormat.GENERAL_FORMAT CellFormat > // no longer uses the SIMPLE_NUMBER CellFormatter. > // Note that the simpleValue()/SIMPLE_NUMBER CellFormatter format >@@ -125,7 +127,9 @@ > */ > public CellNumberFormatter(String format) { > super(format); >- >+ >+ dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); >+ > CellNumberPartHandler ph = new CellNumberPartHandler(); > StringBuffer descBuf = CellFormatPart.parseFormat(format, CellFormatType.NUMBER, ph); > >@@ -259,7 +263,6 @@ > } > fmtBuf.append('E'); > placeZeros(fmtBuf, exponentSpecials.subList(2, exponentSpecials.size())); >- DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); > decimalFmt = new DecimalFormat(fmtBuf.toString(), dfs); > printfFmt = null; > } >@@ -410,7 +413,7 @@ > public void formatValue(StringBuffer toAppendTo, Object valueObject) { > double value = ((Number) valueObject).doubleValue(); > value *= scale; >- >+ dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); //reload dfs as the locale might have changed > // For negative numbers: > // - If the cell format has a negative number format, this method > // is called with a positive value and the number format has >@@ -465,6 +468,7 @@ > // records chars already deleted > BitSet deletedChars = new BitSet(); > int adjust = 0; >+ String groupSeparetor =String.valueOf(dfs.getGroupingSeparator()); > for (Special s : specials) { > int adjustedPos = s.pos + adjust; > if (!deletedChars.get(s.pos) && output.charAt(adjustedPos) == '#') { >@@ -478,7 +482,7 @@ > switch (nextChange.getOp()) { > case CellNumberStringMod.AFTER: > // ignore adding a comma after a deleted char (which was a '#') >- if (nextChange.getToAdd().equals(",") && deletedChars.get(s.pos)) { >+ if (nextChange.getToAdd().equals(groupSeparetor) && deletedChars.get(s.pos)) { > break; > } > output.insert(modPos + 1, nextChange.getToAdd()); >@@ -710,8 +714,11 @@ > private void writeInteger(StringBuffer result, StringBuffer output, > List<Special> numSpecials, Set<CellNumberStringMod> mods, > boolean showCommas) { >- >- int pos = result.indexOf(".") - 1; >+ String decimalSeparator = String.valueOf(dfs.getDecimalSeparator()); >+ if(output.indexOf(".")>0){ >+ output.replace(output.indexOf("."), output.indexOf(".")+1, decimalSeparator); >+ } >+ int pos = result.indexOf(decimalSeparator) - 1; > if (pos < 0) { > if (exponent != null && numSpecials == integerSpecials) { > pos = result.indexOf("E") - 1; >@@ -748,8 +755,9 @@ > output.setCharAt(s.pos, (zeroStrip ? ' ' : resultCh)); > lastOutputIntegerDigit = s; > } >+ String groupSeparator = String.valueOf(dfs.getGroupingSeparator()); > if (followWithComma) { >- mods.add(insertMod(s, zeroStrip ? " " : ",", CellNumberStringMod.AFTER)); >+ mods.add(insertMod(s, zeroStrip ? " " : groupSeparator, CellNumberStringMod.AFTER)); > followWithComma = false; > } > digit++; >@@ -764,7 +772,7 @@ > if (showCommas) { > while (pos > 0) { > if (digit > 0 && digit % 3 == 0) { >- extraLeadingDigits.insert(pos, ','); >+ extraLeadingDigits.insert(pos, dfs.getGroupingSeparator()); > } > digit++; > --pos; >@@ -778,7 +786,8 @@ > int digit; > int strip; > if (fractionalSpecials.size() > 0) { >- digit = result.indexOf(".") + 1; >+ String decimalSeparator = String.valueOf(dfs.getDecimalSeparator()); >+ digit = result.indexOf(decimalSeparator) + 1; > if (exponent != null) { > strip = result.indexOf("e") - 1; > } else { >@@ -816,4 +825,5 @@ > private static Special lastSpecial(List<Special> s) { > return s.get(s.size() - 1); > } >+ > } >Index: src/java/org/apache/poi/ss/usermodel/DataFormatter.java >=================================================================== >--- src/java/org/apache/poi/ss/usermodel/DataFormatter.java (revision 288774) >+++ src/java/org/apache/poi/ss/usermodel/DataFormatter.java (working copy) >@@ -323,6 +323,7 @@ > try { > // Ask CellFormat to get a formatter for it > CellFormat cfmt = CellFormat.getInstance(formatStr); >+ LocaleUtil.setUserLocale(locale); //propagate the current locale > // CellFormat requires callers to identify date vs not, so do so > Object cellValueO = Double.valueOf(cellValue); > if (DateUtil.isADateFormat(formatIndex, formatStr) && >Index: src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java >=================================================================== >--- src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java (revision 288774) >+++ src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java (working copy) >@@ -131,6 +131,22 @@ > assertEquals("1'234,57", dfDE.formatRawCellContents(1234.567, -1, "#'##0.00")); > assertEquals("1 234,57", dfDE.formatRawCellContents(1234.567, -1, "# ##0.00")); > } >+ /** >+ * Test that we use the specified locale when deciding >+ * how to format numbers with three part custom formats like #,##0 ;(#,##0) ;\"-\"" >+ */ >+ @Test >+ public void testGroupingWithThreeParts() { >+ DataFormatter dfIT = new DataFormatter(Locale.ITALY); >+ DataFormatter dfUS = new DataFormatter(Locale.US); >+ >+ assertEquals("486,859,118.5",dfUS.formatRawCellContents(486859118.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\"")); >+ assertEquals("-",dfIT.formatRawCellContents(0, -1, "#,##0 ;(#,##0) ;\"-\"")); >+ assertEquals("486.859.118",dfIT.formatRawCellContents(486859118, -1, "#,##0 ;(#,##0) ;\"-\"")); >+ assertEquals("(486.859.118)",dfIT.formatRawCellContents(-486859118, -1, "#,##0 ;(#,##0) ;\"-\"")); >+ assertEquals("786.859.118,5",dfIT.formatRawCellContents(786859118.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\"")); >+ assertEquals("0,5",dfIT.formatRawCellContents(.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\"")); >+ } > > /** > * Ensure that colours get correctly
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 60040
:
34174
|
34175
|
34813
| 34903 |
38763