-2

I have a double (e.g: 123.45) and I need generate a string like 0000012345.

I'm trying to do it using only String.format (Formatter). I could use DecimalFormat and then replaceAll, but I was wondering if I could achieve the same result using formatter. The reason is that I will use it with FormatterLineAggregator (spring batch).

[EDIT] - Emphasis on the question reason. Tagged as spring batch also. Included link to spring batch documentation.

From Spring Batch doc: "... The underlying implementation is built using the same Formatter added as part of Java 5. The Java Formatter is based on the printf functionality of the C programming language. Most details on how to configure a formatter can be found in the javadoc of Formatter.".

Spring Batch itemWriter link

Bob Rivers
  • 5,261
  • 6
  • 47
  • 59
  • You could use DecimalFormat *without* 'replaceAll()'. Why the fixation on 'String.format()'? – user207421 Jun 29 '14 at 00:02
  • @EJP Just read que question. It's clearly stated that is because spring batch requires it. From it's manual: "The underlying implementation is built using the same Formatter added as part of Java 5. The Java Formatter is based on the printf functionality of the C programming language. Most details on how to configure a formatter can be found in the javadoc of Formatter". – Bob Rivers Jun 29 '14 at 11:37
  • possible duplicate of [Spring Batch : PassThroughFieldExtractor with BigDecimal formatting](http://stackoverflow.com/questions/21407486/spring-batch-passthroughfieldextractor-with-bigdecimal-formatting) – Luca Basso Ricci Jun 29 '14 at 15:20

1 Answers1

0

Could you stash the value into a Formattable and use Formattable#formatTo to generate the desired output?

As far as I know there's no way to suppress the decimal separator in a standard String.format("%f") output. Even if you add a DecimalFormatSymbolsProvider class, it must still return a char value from DecimalFormatSymbols.html#getDecimalSeparator().

grkvlt
  • 2,577
  • 1
  • 21
  • 38