Search in sources :

Example 36 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project ORCID-Source by ORCID.

the class FundingsController method getAmountAsBigDecimal.

/**
     * Transforms a string into a BigDecimal
     * 
     * @param amount
     * @param locale
     * @return a BigDecimal containing the given amount
     * @throws Exception
     *             if the amount cannot be correctly parse into a BigDecimal
     * */
public BigDecimal getAmountAsBigDecimal(String amount, Locale locale) throws Exception {
    try {
        ParsePosition parsePosition = new ParsePosition(0);
        DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
        DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
        /**
             * When spaces are allowed, the grouping separator is the character
             * 160, which is a non-breaking space So, lets change it so it uses
             * the default space as a separator
             * */
        if (symbols.getGroupingSeparator() == 160) {
            symbols.setGroupingSeparator(' ');
        }
        numberFormat.setDecimalFormatSymbols(symbols);
        Number number = numberFormat.parse(amount, parsePosition);
        if (number == null || parsePosition.getIndex() != amount.length()) {
            throw new Exception();
        }
        return new BigDecimal(number.toString());
    } catch (Exception e) {
        throw e;
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ParsePosition(java.text.ParsePosition)

Example 37 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project lucene-solr by apache.

the class TestNumberFormatTransformer method testTransformRow_SingleNumber.

@SuppressWarnings("unchecked")
@Test
public void testTransformRow_SingleNumber() {
    char GERMAN_GROUPING_SEP = new DecimalFormatSymbols(Locale.GERMANY).getGroupingSeparator();
    List<Map<String, String>> l = new ArrayList<>();
    l.add(createMap("column", "num", NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER));
    l.add(createMap("column", "localizedNum", NumberFormatTransformer.FORMAT_STYLE, NumberFormatTransformer.NUMBER, NumberFormatTransformer.LOCALE, "de-DE"));
    Context c = getContext(null, null, null, Context.FULL_DUMP, l, null);
    Map<String, Object> m = createMap("num", "123" + GROUPING_SEP + "567", "localizedNum", "123" + GERMAN_GROUPING_SEP + "567");
    new NumberFormatTransformer().transformRow(m, c);
    assertEquals(new Long(123567), m.get("num"));
    assertEquals(new Long(123567), m.get("localizedNum"));
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

Example 38 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project maven-plugins by apache.

the class InvokerReport method executeReport.

protected void executeReport(Locale locale) throws MavenReportException {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
    percentFormat = new DecimalFormat(getText(locale, "report.invoker.format.percent"), symbols);
    secondsFormat = new DecimalFormat(getText(locale, "report.invoker.format.seconds"), symbols);
    Sink sink = getSink();
    sink.head();
    sink.title();
    sink.text(getText(locale, "report.invoker.result.title"));
    sink.title_();
    sink.head_();
    sink.body();
    sink.section1();
    sink.sectionTitle1();
    sink.text(getText(locale, "report.invoker.result.title"));
    sink.sectionTitle1_();
    sink.paragraph();
    sink.text(getText(locale, "report.invoker.result.description"));
    sink.paragraph_();
    sink.section1_();
    // ----------------------------------
    // build buildJob beans
    // ----------------------------------
    File[] reportFiles = ReportUtils.getReportFiles(reportsDirectory);
    if (reportFiles.length <= 0) {
        getLog().info("no invoker report files found, skip report generation");
        return;
    }
    List<BuildJob> buildJobs = new ArrayList<BuildJob>(reportFiles.length);
    for (File reportFile : reportFiles) {
        try {
            BuildJobXpp3Reader reader = new BuildJobXpp3Reader();
            buildJobs.add(reader.read(ReaderFactory.newXmlReader(reportFile)));
        } catch (XmlPullParserException e) {
            throw new MavenReportException("Failed to parse report file: " + reportFile, e);
        } catch (IOException e) {
            throw new MavenReportException("Failed to read report file: " + reportFile, e);
        }
    }
    // ----------------------------------
    // summary
    // ----------------------------------
    constructSummarySection(buildJobs, locale);
    // ----------------------------------
    // per file/it detail
    // ----------------------------------
    sink.section2();
    sink.sectionTitle2();
    sink.text(getText(locale, "report.invoker.detail.title"));
    sink.sectionTitle2_();
    sink.section2_();
    // detail tests table header
    sink.table();
    sink.tableRow();
    // -------------------------------------------
    // name | Result | time | message
    // -------------------------------------------
    sinkTableHeader(sink, getText(locale, "report.invoker.detail.name"));
    sinkTableHeader(sink, getText(locale, "report.invoker.detail.result"));
    sinkTableHeader(sink, getText(locale, "report.invoker.detail.time"));
    sinkTableHeader(sink, getText(locale, "report.invoker.detail.message"));
    sink.tableRow_();
    for (BuildJob buildJob : buildJobs) {
        renderBuildJob(buildJob, locale);
    }
    sink.table_();
    sink.body_();
    sink.flush();
    sink.close();
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) Sink(org.apache.maven.doxia.sink.Sink) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) BuildJobXpp3Reader(org.apache.maven.plugins.invoker.model.io.xpp3.BuildJobXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) BuildJob(org.apache.maven.plugins.invoker.model.BuildJob) IOException(java.io.IOException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 39 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project poi by apache.

the class DataFormatter method createIntegerOnlyFormat.

// Some custom formats
/**
     * @return a <tt>DecimalFormat</tt> with parseIntegerOnly set <code>true</code>
     */
private static DecimalFormat createIntegerOnlyFormat(String fmt) {
    DecimalFormatSymbols dsf = DecimalFormatSymbols.getInstance(Locale.ROOT);
    DecimalFormat result = new DecimalFormat(fmt, dsf);
    result.setParseIntegerOnly(true);
    return result;
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat)

Example 40 with DecimalFormatSymbols

use of java.text.DecimalFormatSymbols in project lucene-solr by apache.

the class SimpleTextDocValuesReader method getSorted.

@Override
public SortedDocValues getSorted(FieldInfo fieldInfo) throws IOException {
    final OneField field = fields.get(fieldInfo.name);
    // valid:
    assert field != null;
    final IndexInput in = data.clone();
    final BytesRefBuilder scratch = new BytesRefBuilder();
    final DecimalFormat decoder = new DecimalFormat(field.pattern, new DecimalFormatSymbols(Locale.ROOT));
    final DecimalFormat ordDecoder = new DecimalFormat(field.ordPattern, new DecimalFormatSymbols(Locale.ROOT));
    return new SortedDocValues() {

        int doc = -1;

        @Override
        public int nextDoc() throws IOException {
            return advance(docID() + 1);
        }

        @Override
        public int docID() {
            return doc;
        }

        @Override
        public long cost() {
            return maxDoc;
        }

        int ord;

        @Override
        public int advance(int target) throws IOException {
            for (int i = target; i < maxDoc; ++i) {
                in.seek(field.dataStartFilePointer + field.numValues * (9 + field.pattern.length() + field.maxLength) + i * (1 + field.ordPattern.length()));
                SimpleTextUtil.readLine(in, scratch);
                try {
                    ord = (int) ordDecoder.parse(scratch.get().utf8ToString()).longValue() - 1;
                } catch (ParseException pe) {
                    throw new CorruptIndexException("failed to parse ord", in, pe);
                }
                if (ord >= 0) {
                    return doc = i;
                }
            }
            return doc = NO_MORE_DOCS;
        }

        @Override
        public boolean advanceExact(int target) throws IOException {
            this.doc = target;
            in.seek(field.dataStartFilePointer + field.numValues * (9 + field.pattern.length() + field.maxLength) + target * (1 + field.ordPattern.length()));
            SimpleTextUtil.readLine(in, scratch);
            try {
                ord = (int) ordDecoder.parse(scratch.get().utf8ToString()).longValue() - 1;
            } catch (ParseException pe) {
                throw new CorruptIndexException("failed to parse ord", in, pe);
            }
            return ord >= 0;
        }

        @Override
        public int ordValue() {
            return ord;
        }

        final BytesRefBuilder term = new BytesRefBuilder();

        @Override
        public BytesRef lookupOrd(int ord) throws IOException {
            if (ord < 0 || ord >= field.numValues) {
                throw new IndexOutOfBoundsException("ord must be 0 .. " + (field.numValues - 1) + "; got " + ord);
            }
            in.seek(field.dataStartFilePointer + ord * (9 + field.pattern.length() + field.maxLength));
            SimpleTextUtil.readLine(in, scratch);
            assert StringHelper.startsWith(scratch.get(), LENGTH) : "got " + scratch.get().utf8ToString() + " in=" + in;
            int len;
            try {
                len = decoder.parse(new String(scratch.bytes(), LENGTH.length, scratch.length() - LENGTH.length, StandardCharsets.UTF_8)).intValue();
            } catch (ParseException pe) {
                throw new CorruptIndexException("failed to parse int length", in, pe);
            }
            term.grow(len);
            term.setLength(len);
            in.readBytes(term.bytes(), 0, len);
            return term.get();
        }

        @Override
        public int getValueCount() {
            return (int) field.numValues;
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) IndexInput(org.apache.lucene.store.IndexInput) ParseException(java.text.ParseException)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)133 DecimalFormat (java.text.DecimalFormat)93 NumberFormat (java.text.NumberFormat)14 Locale (java.util.Locale)13 ParseException (java.text.ParseException)8 BigDecimal (java.math.BigDecimal)7 Currency (java.util.Currency)7 ObjectInputStream (java.io.ObjectInputStream)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 ParsePosition (java.text.ParsePosition)5 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)5 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)5 IndexInput (org.apache.lucene.store.IndexInput)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 BytesRef (org.apache.lucene.util.BytesRef)4 ArrayList (java.util.ArrayList)3