How to break numbers in java?

I am trying to split the number, but don’t know how to do it.

I referred to this link.

stack overflow

Suppose I have a number 12345678.

using the link above, I am breaking into 3 places. So the way out becomes . 123 456 78

But I want this as And I want to take a line that has been split in the form . 12 345 678 12.345.678

Can someone help me?

+1
source share
5 answers

java.text package provides all reasonable number formatting options

    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 usually we can use a shorter version

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

which creates the same line

12.345.678

( ). , /

NumberFormat.getNumberInstance().format(number);
+10

- , .

StringBuffer, "Hello World" Java

12345678

87654321

876 543 21

12 345 678

, 3s, 3 , , , .

, , , length%3 .

+2

, (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;
  }  
0

, int :

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

:

12.345.678
0

String, StringBuilder StringBuffer. :

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());
    }
}

:

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();
    }
}

, , , , "split" , . (: 12345678 : 1.1234.5.67.8 , "split" {1,4,1,2,1}).

0

Source: https://habr.com/ru/post/1598155/


All Articles