I have some code that assumes an input will not exceed 6 digits. It takes that value, pads it with leading 0's, and then prefixes it with "999", like so:
String.format("999%06d", 123); // => "999000123"
The expected input is now overflowing that 6-digit maximum, into the 7 digits. This produces formatted output of 10 characters (e.g. String.format("999%06d", 1000000); // => "9991000000"
), which breaks things for us.
Question: Is it possible to specify this string format so that it will maintain identical logic as above, but instead of always prefixing with leading "999", it will only prefix with TWO leading "9"s if the input is 7 digits long (while still prefixing with THREE leading "9"s if the input is <= 6 digits long)?
To help illustrate, these are the inputs/outputs we would desire when the value increments from 6 to 7 digits:
(Input) => (Formatted Output)
1 => "999000001"
999999 => "999999999"
1000000 => "991000000"
(I know there are other ways to accomplish this, and those are helpful, but our situation is atypical so I'm asking if this can be done in a single String.format
call)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…