0

I am using template expressions for generating files. For example:

def generateStuff(MyObject in) {
    '''
    This is the wrong value: «calculatedoubleValue(in.doubleValue)»
    '''
}

The value of doubleValue is an double. But the generator produces a comma instead of an point, as delimiter.

I also tried using DecimalFormat, for example:

def generateStuff(MyObject in) {
    var df = new DecimalFormat("0.000");
    var calculated = calculatedoubleValue(in.doubleValue)
    '''
    This is the wrong value: «df.format(calculated)»
    '''
}

But unfortunately it still produces a comma. I wonder, because it only happens to a few values and not to all, allthough I am only working with doubles. Another strange thing is, that it produces points, when debugging (Runtime Eclipse Application) but commas after I export the application as an Eclipse product.

What could be the cause for this?

John
  • 795
  • 3
  • 15
  • 38

2 Answers2

0

Within template expressions toString() is invoked on any values (see StringConcatenation). Double.toString will always use '.', so the issue must be somewhere else. Maybe the problem is in your 'calculatedDouble' method?

Sven Efftinge
  • 3,065
  • 17
  • 17
  • Sven, thank you very much for the quick answer. I wonder why it works, during debugging (Runtime Eclipse Application) but not after exporting the Eclipse product. Any idea? – John Feb 23 '16 at 10:03
0

Although I don't have much info about your environment, based on your example code you do not directly use Double.toString() (that always uses . as decimal separator) but DecimalFormat which is locale-sensitive.

When DecimalFormat is constructed by using the DecimalFormat(String), it will use the default "format locale". In some locales the decimal separator is . (e.g. in English) but in other locales it is , (e.g. in Hungarian). See the API docs for more details.

Proposed solutions to your problem:

  • Ensure that the application uses the same locale in each case.
  • Set the locale-specific formatting options explicitly.

Real code:

@Test
public void testDecimalFormat() {
    assertEquals("0.000", new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(0.0));
    assertEquals("0,000", new DecimalFormat("0.000", new DecimalFormatSymbols(new Locale("hu"))).format(0.0));
}
snorbi
  • 2,590
  • 26
  • 36