gpt4 book ai didi

java - String.format() 获取最大值

转载 作者:行者123 更新时间:2023-12-01 18:43:03 25 4
gpt4 key购买 nike

我实现了一个计数器方法,它始终返回一个递增的数字。但用户可以给出愿望format 、 2 位数字、 3 位数字或任何他想要的数字。格式是字符串的标准 String.format() 类型,如 %02d%5d。当达到最大值时,计数器应重置为 0。

如何找出给定 format 可以表示的最大值?

int counter = 0;
private String getCounter(String format){
if(counter >= getMaximum(format)){
counter = 0;
}
else {
counter++;
}
return String.format(format, counter);
}

private int getMaximum(String format){
//TODO ???
//Format can be %02d => should return 100
//Format can be %05d => should return 100000

}

最佳答案

尚未验证此代码,但类似的内容应该适用于错误检查

    String str = fullresultText.replace ("%", "").replace("d", "");
maxVal = Math.pow (10, Integer.parseInt (str));

关于java - String.format() 获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com