1

I am trying to split number but don't know how to do this.

I referred this link.

https://stackoverflow.com/a/12297231/1395259

Suppose I have a number 12345678.

using above link I am splitting by 3 places.So the output becomes 123 456 78.

But I want it as 12 345 678 And I want to take the string that was split in the form 12.345.678 .

Can anybody help me please??

Community
  • 1
  • 1
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85

5 Answers5

10

java.text package provides all reasonable options for formatting numbers

    DecimalFormat f = new DecimalFormat("#,###");
    DecimalFormatSymbols fs = f.getDecimalFormatSymbols();
    fs.setGroupingSeparator('.');
    f.setDecimalFormatSymbols(fs);
    String s = f.format(12345678);
    System.out.println(s);

output

12.345.678

using DecimalFormat directly is very flexible, but typically we can use a shorter version

String s = NumberFormat.getNumberInstance(Locale.GERMAN).format(12345678);

which produces the same string

12.345.678

Different countries have different rules for formatting numbers (and dates). Usually we want our programs to be internationalized / localized and use default locale version

NumberFormat.getNumberInstance().format(number);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

One lazy way is to reverse the string, apply above method, and then reverse it again.

You can use StringBuffer's Reverse Function, as shown in Reverse each individual word of "Hello World" string with Java

12345678

87654321

876 543 21

12 345 678

I am assuming of course that you want to split by 3s and the group with <3 digits left appears in the start rather than the end as in the method you link.

The not so lazy way would be to use string length to adapt the method you link to start with length%3 characters.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

Using the solution from your link i would rephrase that as following (TESTED!):

  public String[] splitStringEvery(String s, int interval) {
    int arrayLength = (int) Math.ceil(((s.length() / (double)interval)));
    String[] result = new String[arrayLength];

    int j = s.length();
    int lastIndex = result.length;
    for (int i = lastIndex - 1; i > 0; i--) {
        result[i] = s.substring(j - interval, j);
        j -= interval;
    } //Add the last bit
    result[0] = s.substring(0, j);

    return result;
  }  
GeorgeVremescu
  • 1,253
  • 7
  • 12
0

Here is a method that splits an int value and returns an String in the specified format:

public static String split( int n ) {
    String result = "", s = Integer.toString( n );
    while ( s.length() > 3 ) {
        result = s.substring( s.length() -3, s.length() ) + ((result.length() > 0)? "." + result : "" );
        s = s.substring( 0, s.length() -3 );
    }
    return s + "." + result;
}

Input:

12345678

Output:

12.345.678
arutaku
  • 5,937
  • 1
  • 24
  • 38
  • NEVER join strings in a loop together. Use a StringBuilder instead. Also use an integer to tell where in the string you are instead of creating a new string for `s` over and over again. – Nemo64 Jan 24 '13 at 08:05
0

If it's a String, use StringBuilder or StringBuffer. Here's the code:

public class SplitNumber {
    public static void main(String[] args){
        int number = 12345678;

        String numberStrBefore = Integer.toString(number);

        StringBuffer numberStrAfter = new StringBuffer();
        numberStrAfter.append(numberStrBefore.charAt(0));
        numberStrAfter.append(numberStrBefore.charAt(1));
        numberStrAfter.append('.');
        numberStrAfter.append(numberStrBefore.charAt(2));
        numberStrAfter.append(numberStrBefore.charAt(3));
        numberStrAfter.append(numberStrBefore.charAt(4));
        numberStrAfter.append('.');
        numberStrAfter.append(numberStrBefore.charAt(5));
        numberStrAfter.append(numberStrBefore.charAt(6));
        numberStrAfter.append(numberStrBefore.charAt(7));

        System.out.println("Number Before: " + numberStrBefore);
        System.out.println("Number After: " + numberStrAfter.toString());
    }
}

And here is the same thing with a method:

public class SplitNumber {
    public static void main(String[] args){
        int number = 12345678;
        int[] split = {2,3,3}; //How to split the number

        String numberStrAfter = insertDots(number, split);

        System.out.println("Number Before: " + number);
        System.out.println("Number After: " + numberStrAfter);
    }

    public static String insertDots(int number, int[] split){
        StringBuffer numberStrAfter = new StringBuffer();
        String numberStr = Integer.toString(number);

        int currentIndex = 0;       
        for(int i = 0; i < split.length; i++){
            for(int j = 0; j < split[i]; j++){
                numberStrAfter.append(numberStr.charAt(currentIndex));
                currentIndex++;
            }
            numberStrAfter.append('.');
        }
        numberStrAfter.deleteCharAt(numberStrAfter.length()-1); //Removing last "."

        return numberStrAfter.toString();
    }
}

This version, with the method, allows you to split any number into any format that you want, simply change the "split" variable into the format that you want to split the string into. (Ex: Splitting 12345678 into: 1.1234.5.67.8 would mean that "split" must be set to {1,4,1,2,1}).

Steve
  • 1,469
  • 2
  • 11
  • 11