String.format()获得最大值 [英] String.format() get maximum value

查看:393
本文介绍了String.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 ???
//格式可以是%02d =>应该返回100
//格式可以是%05d =>应该返回100000



$ div class =h2_lin>解决方案

没有验证这个代码,但沿线的东西应该与错误检查到位

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


I implemented a counter method that returns always an incremented number. But the user can give wished format, 2 digits, 3 digits or whatever he wants. The format is the standard String.format() type of String like %02d or %5d. When the maximum value is reached, the counter should be reset to 0.

How can I find out the max value that can be represented with the given 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

}

解决方案

Haven't validated this code, but something along the lines of this should work with erro checking in place

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

这篇关于String.format()获得最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆