Skip to content

Instantly share code, notes, and snippets.

@namuan
Created January 15, 2014 10:48
Show Gist options
  • Save namuan/8434231 to your computer and use it in GitHub Desktop.
Save namuan/8434231 to your computer and use it in GitHub Desktop.
Convert number to words
@Grapes(
@Grab(group='com.ibm.icu', module='icu4j', version='52.1')
)
import com.ibm.icu.text.*
import java.util.Locale
import com.ibm.icu.text.RuleBasedNumberFormat
public String spellOut(num) {
NumberFormat formatter = new RuleBasedNumberFormat(RuleBasedNumberFormat.SPELLOUT)
String result = formatter.format(num)
result
}
assert "one hundred" == spellOut(100)
assert "one hundred one" == spellOut(101)
assert "two thousand seven hundred eighteen point two eight" == spellOut(2718.28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment