I have the following JSON example below:
{
"value": 946.2,
"description": "O valor é R$ 946,20."
}
I need to use MessageFormat to JUnit test this JSON example, but I get an invalid value because my Locale is not in english. If I change my Locale to english instead of brazilian portuguese I get an invalid description because the currency value is displayed in English.
Here's my code:
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Locale;
Locale.setDefault(Locale.ENGLISH);
System.out.println(MessageFormat.format("""
'{'
"value": {0},
"description": "O valor é {0,number,currency}."
'}'
""", new BigDecimal(946.2)));
How can I format the value or the description in order to get the JSON as displayed above?
Sure, no problem! But:
Locale.setDefault is harsh! (for test it can be tolerable.... but i don't want to look up that bug..)MessageFormat... it uses StringBuffer internally(Mr. performance killer & memory dumper).)As long as you can handle the "complexity", it can be done like:
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class MainSimple {
// Brazilian Locale:
private static final Locale LOCALE_PT_BR = new Locale.Builder().
setLanguage("pt").
setRegion("BR").
build();
// US number format:
private static final NumberFormat DEFAULT_NUMBER_FMT = NumberFormat.getNumberInstance(Locale.US);
public static void main(String[] args) {
// MessageFormat instance, with format (with 2! parameters) and Locale:
MessageFormat mainFmt = new MessageFormat("""
'{'
"value": {0},
"description": "O valor é {1,number,currency}. <-- trick 1 "
'}'
""", LOCALE_PT_BR);
final BigDecimal num = new BigDecimal(946.2);
// trick 2:
Object[] params = {DEFAULT_NUMBER_FMT.format(num), num};
// do it:
System.out.println(mainFmt.format(params));
}
}
Outline:
MessageFormat has a constructor with locale parameter! (we use that, and call it outer format/locale(mainFmt)){0} and {1,number,currency}String, the second as had. (Object[] params = {DEFAULT_NUMBER_FMT.format(num), num})Prints:
{
"value": 946.2,
"description": "O valor é R$ 946,20."
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With