Why does the NumberFormat returns blank fields? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Why does the NumberFormat returns blank fields? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

So a few days ago I encountered a weird problem however, I didn’t change any that kind of code. The problem is the format I’m getting from my method which I used for years. All commas are now spaces (blank fields) and I have no idea what is causing this.

public static String toFancyCost(int num) {
    return NumberFormat.getInstance().format((Integer) num);
}

Before even this happened the String I received was looking like for example 2,181,273 and not like 2 181 273.

Answer

You must have changed your system locale by accident. The implementation of NumberFormat.getInstance() (on 1.8.0_131):

public final static NumberFormat getInstance() {
    return getInstance(Locale.getDefault(Locale.Category.FORMAT), NUMBERSTYLE);
}

It uses formatting specified by the default locale. and the java docs on Locale.getDefault say:

The Java Virtual Machine sets the default locale during startup based on the host environment. It is used by many locale-sensitive methods if no locale is explicitly specified. It can be changed using the setDefault method.

If you were to use NumberFormat.getInstance(Locale) you can specify which locale the NumberFormat should use.

We are here to answer your question about Why does the NumberFormat returns blank fields? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji