String Convert With Thousand Separator in Java

WOW, ONE MORE THING TO LEARN

in this java tutorial, we will learn how to convert a String with Thousand Separator in Java. Why a String has to be with a thousand separator that’s a million-dollar question. There are millions of possibilities but let’s take it as a simple requirement that we have a String that looks like a number like "123456543".

Now, this String We need to show with Thousand Separator Like "123,456,543" or Maybe like this "123,456,543.00" Even it needs to be shown like this "$123,456,543.00" .

If your requirements like this please follow this article either please put your comments below and let me have the chance to solve your problem.

So let’s solve the problem. Simple Algorithm Like this,

  • First, get the String
  • Convert it to INT or DOUBLE According to the data you have
  • Then Use Java.text.NumberFormat to Get the NumberFormat Instance
  • Format your Converted Double or Int To String with Thousand Separator.

Example 1 – Convert String With Thousand Separator

Code Output for Example 1 – a String with thousand separator

Okay, for example, -1, we have a String that looks like an Integer number. that’s why we converted our String to int using Integer.parseInt().

After Conversion, we have our int. by using NumberFormat.getNumberInstance() we will format our int to String with a thousand separators.

Note: If we use NumberFormat.getCurrencyInstance() we will have our String with currency code. I have shown two examples we can specify our locale to get our expected currency code with our String like the first example with $ (dollar Sign for US dollar).

The Second Example we have is default locale, it’s showing MYR (Malaysian Ringgit) because of the time I am writing this, I am in Malaysia. (LOL) .

java.lang.NumberFormatException will happen if you want to convert "123456543.00" with the above code.

Now the question is How to Convert String with a thousand separator if the String looks like a float or double value.

here is the solution for your string that looks like a double or float.

Example 2 – String Convert With Thousand Separator.

The output of Example 2 – String With Thousand Separators

Please Notice above that when we use NumberFormat.getInstance() the zeros were missing when it converted.

The output is "1,234,256,790" from our  input "1234256790.00" . The zeros will be missing if you have zeros in the tail.

I tried all the NumberFormat built-in methods all the methods give me the same kind of output. If you have some value without zeros like ".45" then it will convert with ".45".

As you noticed in the example that if we use NumberFormat.getCurrencyInstance() then it will convert with zeros. No issue.

Okay, now I have a funny requirement is that they don’t want to show the currency code that appearing in front but they need to show the zeros. I tried all the methods from NumberFormat. it can’t meet the requirements.

So I came up with these hacks.

Example 3 – String with thousand separators

The output of Example 3 – String with Thousand Separators.

I don’t know any other hacks for this requirement so I came up with this. If you know any other hacks or any suitable method that can meet this requirement please let us know. Thanks in advance.

Clone / Download the Sample Project from GitHub : Download / Clone Sample Project

I hope this will help you if you have the same requirement as I mentioned above. Thank you so much for visiting my website.

Leave a Reply

avatar
  Subscribe  
Notify of