0-filled (non-zero) method

background

When dealing with employee numbers etc., there was a slight hole when I was investigating how to match the final number of digits with any digit, so make a note of it.

Class used

String.format("%xyz" , n); % = Regular expression x = the number you want to fill y = number of digits to fill (including n) z = Decimal or other decimal acronym (this time d)

String.replace (convert what to what);

I tried it

public class strPractice02 {

    public static void main(String[] args) {
        
        //Add 4 digits of zero before 9
        String str = String.format("%05d", 9);
        System.out.println(str);
        
        //Replace with a non-zero character
        String str2 = String.format("%15d", 9);
        System.out.println(str2);
        
        //An alternative if it somehow goes blank
        String str3 = String.format("%5d", 9).replace(" ", "1");
        System.out.println(str3);
        
    }

}

Execution result

00009
              9
11119

Summary

When I was investigating, it was written that even numbers other than 0 could be filled in by substituting them into the x part, but str2, which I tried to fill with 1, had a 14-digit blank. So I replaced the whitespace.

Recommended Posts

0-filled (non-zero) method
Method
Java method
to_i method
java (method)
getRequestDispatcher () method
merge method
Map method
include method
Abstract method
initialize method
List method
puts method
Java method
Class method
save! method
getParameter method
[Java] method
private method
rails method
[Java] method