user3733648
user3733648

Reputation: 1333

Formatting currency without decimals

I am using LSEuroCurrencyFormat like this:

#LSEuroCurrencyFormat("1999","Local")#

which outputs something like:

1.999,00 €

But what I need is that the decimal part should not be displayed or should not be the part of the price after formatting. It should just look like this:

1.999 €

Upvotes: 1

Views: 1927

Answers (1)

Leigh
Leigh

Reputation: 28873

That function uses the standard java rules for formatting Currency, which usually implies two decimal places. As @Adam mentioned, there is nothing built in that will give you that exact result. However, you can use java's number formatting classes, with a custom Locale, for a more robust solution.

For example, you could use the NumberFormat class to grab a currency formatter for the appropriate Locale. Then use its methods to suppress the decimal places. (Obviously, that may result in rounding, depending on the input value.) I do not know the default Locale for your JVM, but was able to produce the desired result using the Locale for Spain, ie es_ES

// Grab currency formatter for the desired Locale
locale = createObject("java", "java.util.Locale").init("es", "ES");
formatter = createObject("java", "java.text.NumberFormat").getCurrencyInstance(locale);

// Suppress decimal places 
formatter.setMaximumFractionDigits(0);
writeDump( formatter.format( javacast("double", 1999)) );

The result is:

1.999 € 

Just keep in mind that java's predefined number and date formats are based on the standard conventions for whatever Locale is used. That includes details like the decimal separator, the currency symbol - even the placement of the currency symbol. While you can change the format however you wish, it is generally best to keep it within the realm of the average user's expectations.

Upvotes: 2

Related Questions