Java数字补前零分块、交替反转求和及动态补零实现问询
Hey there! Let's fix that dynamic leading zero padding problem you're stuck on for your number processing workflow.
First, let's recap your core need for step 1: pad leading zeros so the total length of the number string is a multiple of your variable a. Here are solutions for both approaches you tried, plus a bonus simpler method if you're on Java 11+:
Solution 1: Dynamic String.format
The trick here is to build the format string on the fly using the required total length. Here's how to do it:
int a = 3; long targetNumber = 9056737152L; // Your original number // Convert number to string to get current length String numStr = String.valueOf(targetNumber); int currentLength = numStr.length(); // Calculate total length needed (next multiple of a) int totalLength = ((currentLength + a - 1) / a) * a; // Rounds up to nearest multiple of a // Build the dynamic format string (e.g., "%012d" for a=3 and 10-digit number) String dynamicFormat = String.format("%%0%dd", totalLength); // Apply the format to get padded string String paddedString = String.format(dynamicFormat, targetNumber); System.out.println(paddedString); // Outputs: 009056737152
- The
%%escapes to a single%in the final format string, so%%0%ddbecomes something like%012dwhere 12 is your calculated total length. - The
((currentLength + a -1)/a)*aformula safely rounds up to the next multiple ofa(even if the original length is already a multiple ofa, it stays the same).
Solution 2: Dynamic DecimalFormat
For DecimalFormat, you can generate the pattern string dynamically with the exact number of zeros needed for the total length:
int a = 3; long targetNumber = 9056737152L; String numStr = String.valueOf(targetNumber); int currentLength = numStr.length(); int totalLength = ((currentLength + a - 1) / a) * a; // Create a pattern string with totalLength zeros (e.g., "000000000000" for 12 digits) String pattern = new String(new char[totalLength]).replace('\0', '0'); DecimalFormat df = new DecimalFormat(pattern); String paddedString = df.format(targetNumber); System.out.println(paddedString); // Outputs: 009056737152
- This creates a pattern entirely of zeros, which tells
DecimalFormatto pad leading zeros to reach the specified length.
Bonus: Simplest Method (Java 11+)
If you're using Java 11 or later, the String.padStart method makes this even cleaner:
int a = 3; long targetNumber = 9056737152L; String numStr = String.valueOf(targetNumber); int totalLength = ((numStr.length() + a - 1) / a) * a; String paddedString = numStr.padStart(totalLength, '0'); System.out.println(paddedString); // Outputs: 009056737152
padStartdirectly pads the string with the specified character (here, '0') to reach the desired total length.
All these methods dynamically adapt to any value of a you pass in, so you won't have hardcoded zero counts anymore. Once you have the padded string, you can move on to splitting into chunks and processing the rest of your workflow.
内容的提问来源于stack exchange,提问作者3498BoyZ

