View | Details | Raw Unified | Return to bug 60040
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/format/CellNumberFormatter.java (-10 / +20 lines)
Lines 61-68 Link Here
61
    private final String denominatorFmt;
61
    private final String denominatorFmt;
62
    private final boolean improperFraction;
62
    private final boolean improperFraction;
63
    private final DecimalFormat decimalFmt;
63
    private final DecimalFormat decimalFmt;
64
    private DecimalFormatSymbols dfs;
64
65
65
    // The CellNumberFormatter.simpleValue() method uses the SIMPLE_NUMBER
66
67
	// The CellNumberFormatter.simpleValue() method uses the SIMPLE_NUMBER
66
    // CellFormatter defined here. The CellFormat.GENERAL_FORMAT CellFormat
68
    // CellFormatter defined here. The CellFormat.GENERAL_FORMAT CellFormat
67
    // no longer uses the SIMPLE_NUMBER CellFormatter.
69
    // no longer uses the SIMPLE_NUMBER CellFormatter.
68
    // Note that the simpleValue()/SIMPLE_NUMBER CellFormatter format
70
    // Note that the simpleValue()/SIMPLE_NUMBER CellFormatter format
Lines 125-131 Link Here
125
     */
127
     */
126
    public CellNumberFormatter(String format) {
128
    public CellNumberFormatter(String format) {
127
        super(format);
129
        super(format);
128
130
        
131
        dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
132
        
129
        CellNumberPartHandler ph = new CellNumberPartHandler();
133
        CellNumberPartHandler ph = new CellNumberPartHandler();
130
        StringBuffer descBuf = CellFormatPart.parseFormat(format, CellFormatType.NUMBER, ph);
134
        StringBuffer descBuf = CellFormatPart.parseFormat(format, CellFormatType.NUMBER, ph);
131
135
Lines 259-265 Link Here
259
            }
263
            }
260
            fmtBuf.append('E');
264
            fmtBuf.append('E');
261
            placeZeros(fmtBuf, exponentSpecials.subList(2, exponentSpecials.size()));
265
            placeZeros(fmtBuf, exponentSpecials.subList(2, exponentSpecials.size()));
262
            DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
263
            decimalFmt = new DecimalFormat(fmtBuf.toString(), dfs);
266
            decimalFmt = new DecimalFormat(fmtBuf.toString(), dfs);
264
            printfFmt = null;
267
            printfFmt = null;
265
        }
268
        }
Lines 410-416 Link Here
410
    public void formatValue(StringBuffer toAppendTo, Object valueObject) {
413
    public void formatValue(StringBuffer toAppendTo, Object valueObject) {
411
        double value = ((Number) valueObject).doubleValue();
414
        double value = ((Number) valueObject).doubleValue();
412
        value *= scale;
415
        value *= scale;
413
416
        dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale()); //reload dfs as the locale might have changed
414
        // For negative numbers:
417
        // For negative numbers:
415
        // - If the cell format has a negative number format, this method
418
        // - If the cell format has a negative number format, this method
416
        // is called with a positive value and the number format has
419
        // is called with a positive value and the number format has
Lines 465-470 Link Here
465
        // records chars already deleted
468
        // records chars already deleted
466
        BitSet deletedChars = new BitSet();
469
        BitSet deletedChars = new BitSet();
467
        int adjust = 0;
470
        int adjust = 0;
471
        String groupSeparetor =String.valueOf(dfs.getGroupingSeparator());
468
        for (Special s : specials) {
472
        for (Special s : specials) {
469
            int adjustedPos = s.pos + adjust;
473
            int adjustedPos = s.pos + adjust;
470
            if (!deletedChars.get(s.pos) && output.charAt(adjustedPos) == '#') {
474
            if (!deletedChars.get(s.pos) && output.charAt(adjustedPos) == '#') {
Lines 478-484 Link Here
478
                switch (nextChange.getOp()) {
482
                switch (nextChange.getOp()) {
479
                case CellNumberStringMod.AFTER:
483
                case CellNumberStringMod.AFTER:
480
                    // ignore adding a comma after a deleted char (which was a '#')
484
                    // ignore adding a comma after a deleted char (which was a '#')
481
                    if (nextChange.getToAdd().equals(",") && deletedChars.get(s.pos)) {
485
                    if (nextChange.getToAdd().equals(groupSeparetor) && deletedChars.get(s.pos)) {
482
                        break;
486
                        break;
483
                    }
487
                    }
484
                    output.insert(modPos + 1, nextChange.getToAdd());
488
                    output.insert(modPos + 1, nextChange.getToAdd());
Lines 710-717 Link Here
710
    private void writeInteger(StringBuffer result, StringBuffer output,
714
    private void writeInteger(StringBuffer result, StringBuffer output,
711
            List<Special> numSpecials, Set<CellNumberStringMod> mods,
715
            List<Special> numSpecials, Set<CellNumberStringMod> mods,
712
            boolean showCommas) {
716
            boolean showCommas) {
713
717
    	String decimalSeparator = String.valueOf(dfs.getDecimalSeparator());
714
        int pos = result.indexOf(".") - 1;
718
    	if(output.indexOf(".")>0){
719
    		output.replace(output.indexOf("."), output.indexOf(".")+1, decimalSeparator);
720
    	}
721
        int pos = result.indexOf(decimalSeparator) - 1;
715
        if (pos < 0) {
722
        if (pos < 0) {
716
            if (exponent != null && numSpecials == integerSpecials) {
723
            if (exponent != null && numSpecials == integerSpecials) {
717
                pos = result.indexOf("E") - 1;
724
                pos = result.indexOf("E") - 1;
Lines 748-755 Link Here
748
                output.setCharAt(s.pos, (zeroStrip ? ' ' : resultCh));
755
                output.setCharAt(s.pos, (zeroStrip ? ' ' : resultCh));
749
                lastOutputIntegerDigit = s;
756
                lastOutputIntegerDigit = s;
750
            }
757
            }
758
            String groupSeparator = String.valueOf(dfs.getGroupingSeparator());
751
            if (followWithComma) {
759
            if (followWithComma) {
752
                mods.add(insertMod(s, zeroStrip ? " " : ",", CellNumberStringMod.AFTER));
760
                mods.add(insertMod(s, zeroStrip ? " " : groupSeparator, CellNumberStringMod.AFTER));
753
                followWithComma = false;
761
                followWithComma = false;
754
            }
762
            }
755
            digit++;
763
            digit++;
Lines 764-770 Link Here
764
            if (showCommas) {
772
            if (showCommas) {
765
                while (pos > 0) {
773
                while (pos > 0) {
766
                    if (digit > 0 && digit % 3 == 0) {
774
                    if (digit > 0 && digit % 3 == 0) {
767
                        extraLeadingDigits.insert(pos, ',');
775
                        extraLeadingDigits.insert(pos, dfs.getGroupingSeparator());
768
                    }
776
                    }
769
                    digit++;
777
                    digit++;
770
                    --pos;
778
                    --pos;
Lines 778-784 Link Here
778
        int digit;
786
        int digit;
779
        int strip;
787
        int strip;
780
        if (fractionalSpecials.size() > 0) {
788
        if (fractionalSpecials.size() > 0) {
781
            digit = result.indexOf(".") + 1;
789
        	String decimalSeparator = String.valueOf(dfs.getDecimalSeparator());
790
            digit = result.indexOf(decimalSeparator) + 1;
782
            if (exponent != null) {
791
            if (exponent != null) {
783
                strip = result.indexOf("e") - 1;
792
                strip = result.indexOf("e") - 1;
784
            } else {
793
            } else {
Lines 816-819 Link Here
816
    private static Special lastSpecial(List<Special> s)  {
825
    private static Special lastSpecial(List<Special> s)  {
817
        return s.get(s.size() - 1);
826
        return s.get(s.size() - 1);
818
    }
827
    }
828
    
819
}
829
}
(-)src/java/org/apache/poi/ss/usermodel/DataFormatter.java (+1 lines)
Lines 323-328 Link Here
323
            try {
323
            try {
324
                // Ask CellFormat to get a formatter for it
324
                // Ask CellFormat to get a formatter for it
325
                CellFormat cfmt = CellFormat.getInstance(formatStr);
325
                CellFormat cfmt = CellFormat.getInstance(formatStr);
326
                LocaleUtil.setUserLocale(locale); //propagate the current locale
326
                // CellFormat requires callers to identify date vs not, so do so
327
                // CellFormat requires callers to identify date vs not, so do so
327
                Object cellValueO = Double.valueOf(cellValue);
328
                Object cellValueO = Double.valueOf(cellValue);
328
                if (DateUtil.isADateFormat(formatIndex, formatStr) && 
329
                if (DateUtil.isADateFormat(formatIndex, formatStr) && 
(-)src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java (+16 lines)
Lines 131-136 Link Here
131
        assertEquals("1'234,57", dfDE.formatRawCellContents(1234.567, -1, "#'##0.00"));
131
        assertEquals("1'234,57", dfDE.formatRawCellContents(1234.567, -1, "#'##0.00"));
132
        assertEquals("1 234,57", dfDE.formatRawCellContents(1234.567, -1, "# ##0.00"));
132
        assertEquals("1 234,57", dfDE.formatRawCellContents(1234.567, -1, "# ##0.00"));
133
    }
133
    }
134
    /**
135
     * Test that we use the specified locale when deciding
136
     *   how to format numbers with three part custom formats like #,##0 ;(#,##0) ;\"-\""
137
     */
138
    @Test
139
    public void testGroupingWithThreeParts() {
140
        DataFormatter dfIT = new DataFormatter(Locale.ITALY);
141
        DataFormatter dfUS = new DataFormatter(Locale.US);
142
        
143
        assertEquals("486,859,118.5",dfUS.formatRawCellContents(486859118.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\""));
144
        assertEquals("-",dfIT.formatRawCellContents(0, -1, "#,##0 ;(#,##0) ;\"-\""));
145
        assertEquals("486.859.118",dfIT.formatRawCellContents(486859118, -1, "#,##0 ;(#,##0) ;\"-\""));
146
        assertEquals("(486.859.118)",dfIT.formatRawCellContents(-486859118, -1, "#,##0 ;(#,##0) ;\"-\""));
147
        assertEquals("786.859.118,5",dfIT.formatRawCellContents(786859118.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\""));
148
        assertEquals("0,5",dfIT.formatRawCellContents(.5, -1, "#,##0.0 ;(#,##0.0) ;\"-\""));
149
    }
134
150
135
    /**
151
    /**
136
     * Ensure that colours get correctly
152
     * Ensure that colours get correctly

Return to bug 60040