I want to make a number format like 000"+"000. The rule will be 3 digits "+" 3 digits. I will give you some examples below.
I have tried some codes before i will show them below. I think i have to use NumberFormat class. My codes are below. by the way my number maximum have 6 digits if number has less digits the missing digits(which will be in left), has to be 0.
I tried
NumberFormat numberFormat = new DecimalFormat("000'+'000");
but it gave error which is
Unquoted special character '0' in pattern "000'+'000"
but it was worked when i make
NumberFormat numberFormat = new DecimalFormat("'+'000");
or
NumberFormat numberFormat = new DecimalFormat("000'+'");
So simply i can make number-plus or plus-number but i can't make number(3 digit)-plus-number(3 digit)
I expect to get these outputs for these inputs:
input: 4032
output: 004+032
input : 5
output: 000+005
input: 123450
output: 123+450
input: 10450
output: 010+450
With a trick: change the grouping separator symbol to +
:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setGroupingSeparator('+');
NumberFormat nf = new DecimalFormat("000,000", symbols);
int x = 3250;
System.out.println(nf.format(x));
Result:
003+250
Or use a method like this:
public static String specialFormat(int number) {
NumberFormat nf = new DecimalFormat("000");
return nf.format(number / 1000) + "+" + nf.format(number % 1000);
}
it formats separately the 3 first digits and the 3 last digits and concatenates with a +
in the middle.
Use it:
int x = 5023;
System.out.println(specialFormat(x));
Result:
005+023