You probably want to use NumberFormat.getCurrencyInstance(). This will return a NumberFormat object that uses the standard currency representation for your default Locale (or optionally, the one you pass in).
To right justify, you can use String.padLeft().
Example:
def formatter = java.text.NumberFormat.currencyInstance
def values = [0, 100000000, 9123123.25, 10.20, 1907.23]
def formatted = values.collect { formatter.format(it) }
def maxLen = formatted*.length().max()
println formatted.collect { it.padLeft(maxLen) }.join("\n")
//output
100,000,000.00
10.20
$1,907.23
Answer from ataylor on Stack Overflow Top answer 1 of 2
15
You probably want to use NumberFormat.getCurrencyInstance(). This will return a NumberFormat object that uses the standard currency representation for your default Locale (or optionally, the one you pass in).
To right justify, you can use String.padLeft().
Example:
def formatter = java.text.NumberFormat.currencyInstance
def values = [0, 100000000, 9123123.25, 10.20, 1907.23]
def formatted = values.collect { formatter.format(it) }
def maxLen = formatted*.length().max()
println formatted.collect { it.padLeft(maxLen) }.join("\n")
//output
100,000,000.00
10.20
$1,907.23
2 of 2
5
In grails soemthing like this will format it nicely with comma separators.
<g:formatNumber number="${150000}" type="currency" currencyCode="USD"/>
For right aligning I would use style:
<td style='text-align:right;...'>
Blogger
groovyandgrails.blogspot.com › 2013 › 04 › formatting-number-to-currency-and.html
Groovy and Grails: Formatting Number to Currency and Percentage Strings
import java.text.DecimalFormat def num1 = 654572 //java.lang.Integer def num2 = 0.7507577625 //java.math.BigDecimal //println "num1: " + num1.getClass() //println "num2: " + num2.getClass() println "Format Currency:" def list = [0, 1, 6, 23, 245, 8765, 92309, 654572, 6.57, 23.23, 5.876, 5.875, 5.874 ] list.each{ value -> def pattern = "\$##,###.##" def moneyform = new DecimalFormat(pattern) String output = moneyform.format(value) println(value + " " + pattern + " " + output) } println "Format Percentage:" def list1 = [ 0.7507577625, 1.0423815336, 1.1714391045, 0.8603150483 ] list1.each{ value
Perficient Blogs
blogs.perficient.com › 2015 › 09 › 10 › how to format currency with globally defined groovy script
How to Format Currency with Globally Defined Groovy Script / Blogs / Perficient
May 3, 2018 - In several OSC (Oracle Sales Cloud) based application, there comes a requirement where a currency value needs to be formatted according to the currency format of the locale. In general Groovy compiler supports NumberFormat abstract class which can be called as NumberFormat.getCurrencyInstance() which returns the formatted object.
GitHub
github.com › ricardojmendez › grails-currencies › blob › master › src › groovy › cr › co › arquetipos › currencies › Money.groovy
grails-currencies/src/groovy/cr/co/arquetipos/currencies/Money.groovy at master · ricardojmendez/grails-currencies
return amount.hashCode() + currency.hashCode() } · String toString() { String formatted = decimalFormat.format((BigDecimal)amount) return "${formatted} ${currency?.currencyCode}" } ·
Author ricardojmendez
Appspot
202bug.groovyconsole.appspot.com › script › 5132556307529728
Groovy web console - currency formatting
locale = new Locale("en","GB") currency = Currency.getInstance("EUR") formatter = java.text.NumberFormat.getCurrencyInstance(locale) formatter.setCurrency(currency) println(formatter.format(45.5))
Grails
gsp.grails.org › 6.2.1 › ref › Tags › formatNumber.html
Redirecting...
This page has moved to https://grails.apache.org/docs-legacy-gsp/6.2.1/ref/Tags/formatNumber.html
Grails
gsp.grails.org › latest › ref › Tags › formatNumber.html
Groovy Server Pages (GSP) 6.2.4
Formats a number using the patterns defined by the DecimalFormat class. Also supports attributes used in the JSTL formatNumber tag · Example of formatting an EUR currency amount:
Groovy
groovy-lang.org › syntax.html
The Apache Groovy programming language - Syntax
For example, the Euro currency symbol can be represented with: ... Any Groovy expression can be interpolated in all string literals, apart from single and triple-single-quoted strings. Interpolation is the act of replacing a placeholder in the string with its value upon evaluation of the string.
Radutechhub
radutechhub.com › home › 2020 › 11 › 18 › groovy-26-how-to-get-currency-symbols
Groovy 26 – How to get Currency Symbols – Radu's- Tech Hub
def map = [:]; for (Locale locale : Locale.getAvailableLocales()) { try { Currency currency = Currency.getInstance(locale); map.put(currency, locale); } catch (Exception e) { // no need to handle exception } } Currency currency = Currency.getInstance(CurrencyCode); String country = map.get(currency); String country1 = country.substring(country.lastIndexOf("_") + 1); Locale locale = new Locale("EN", country1); Currency currency1 = Currency.getInstance(locale); String symbol = currency1.getSymbol(map.get(currency)); return symbol;
GitHub
github.com › ticketbis › grails-money › blob › master › src › groovy › com › ticketbis › money › MoneyFormat.groovy
grails-money/src/groovy/com/ticketbis/money/MoneyFormat.groovy at master · ticketbis/grails-money
@groovy.transform.PackageScope · trait MoneyFormat { · abstract Currency getCurrency() abstract BigDecimal getAmount() · DecimalFormat getFormatter(Locale locale = Locale.default, boolean includeCurrency = true) { DecimalFormat formatter ·
Author ticketbis
Apache
grails.apache.org › docs-legacy-gsp › 6.2.1 › ref › Tags › formatNumber.html
Groovy Server Pages (GSP) 6.2.1
Formats a number using the patterns defined by the DecimalFormat class. Also supports attributes used in the JSTL formatNumber tag · Example of formatting an EUR currency amount:
Kodejava
kodejava.org › how-do-i-format-a-number-as-currency-string
How do I format a number as currency string? - Learn Java by Examples
package org.kodejava.text; import java.text.NumberFormat; import java.util.Locale; public class LocaleCurrencyFormat { public static void main(String[] args) { Double number = 1500D; // Format currency for Canada locale in Canada locale, // the decimal point symbol is a comma and currency // symbol is $. NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CANADA); String currency = format.format(number); System.out.println("Currency in Canada : " + currency); // Format currency for Germany locale in German locale, // the decimal point symbol is a dot and currency symbol // is €. format = NumberFormat.getCurrencyInstance(Locale.GERMANY); currency = format.format(number); System.out.println("Currency in Germany: " + currency); } }
Oracle
docs.oracle.com › en › cloud › saas › applications-common › 24a › cgsac › formatting-numbers-and-dates-using-a-formatter.html
Groovy Scripting Reference
December 5, 2023 - Groovy provides the Formatter object that you can use in a text formula expression or anywhere in your scripts that you need for format numbers or dates.
Coderanch
coderanch.com › t › 488634 › languages › Dealing-money-Grails
Dealing with money in Grails (Groovy forum at Coderanch)
Our domain classes use Double class to present money bids. AFAIK, money values should be saved as BigDecimal right?.
Groovy
docs.groovy-lang.org › latest › html › documentation
Groovy Language Documentation
For example, the Euro currency symbol can be represented with: ... Any Groovy expression can be interpolated in all string literals, apart from single and triple-single-quoted strings. Interpolation is the act of replacing a placeholder in the string with its value upon evaluation of the string.
ZetCode
zetcode.com › java › numberformat
Java NumberFormat - formatting numbers and currencies in Java
Java NumberFormat tutorial shows how to format and parse numbers and currencies in Java. We set the number of fractional digits, round numbers, and group digits.
Itecnote
itecnote.com › tecnote › groovy-currency-formatting
Groovy currency formatting
Skip to content · React-native – React Native Error: “Animated.event now requires a second argument for options” · Python – keep on getting “x and y must have same first dimension, but have shapes (100,) and (1, 100)” · Java – Docker WARNING: Published ports are discarded when ...